1 /* -*- mode: c; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
3 /*********************************************************************
4 * Clustal Omega - Multiple sequence alignment
6 * Copyright (C) 2010 University College Dublin
8 * Clustal-Omega is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This file is part of Clustal-Omega.
15 ********************************************************************/
18 * RCS $Id: progress.c 230 2011-04-09 15:37:50Z andreas $
33 #define LOGLEVEL_THRESHOLD LOG_INFO
36 * @brief Allocates a new progress structure and initialises its members.
37 * Free with FreeProgress()
39 * @note Starts the internal stopwatch immediatly!
43 * @param[out] pprProgress
44 * Pointer pointer to progress structure. Progress structure will be
47 * Where to log messages to
49 * What prefix to use for messages
51 * If TRUE carriage return instead of newline will be printed between log messages
54 NewProgress(progress_t **pprProgress, FILE *prFile,
55 char *pcPrefix, bool bPrintCR)
57 assert(NULL!=pprProgress);
59 assert(NULL!=pcPrefix);
61 (*pprProgress) = (progress_t *) CKMALLOC(1*sizeof(progress_t));
62 (*pprProgress)->prFile = prFile;
63 (*pprProgress)->bPrintCR = bPrintCR;
64 (*pprProgress)->pcPrefix = CkStrdup(pcPrefix);
65 strcpy((*pprProgress)->pcLastLogMsg, "\0");
66 (*pprProgress)->prStopwatch = StopwatchCreate();
67 StopwatchZero((*pprProgress)->prStopwatch);
68 StopwatchStart((*pprProgress)->prStopwatch);
72 /*** end: NewProgress() ***/
77 * @brief Frees progress structure and its members
79 * @param[out] pprProgress
80 * Pointer pointer to progress structure
86 FreeProgress(progress_t **pprProgress)
88 (*pprProgress)->prFile = NULL;
89 CKFREE((*pprProgress)->pcPrefix);
90 StopwatchFree((*pprProgress)->prStopwatch);
95 /*** end: FreeProgress() ***/
101 * @brief Prints a progress update (and a carriage return)
103 * @param[in] prProgress
104 * Pointer to the progress structure
106 * Current step number
107 * @param[in] iTotalSteps
109 * @param[in] bForceOutput
110 * If percentage hasn't changed output is normally supressed
111 * normally. Output can be forced with this flag.
115 ProgressLog(progress_t *prProgress,
116 unsigned long int iStep, unsigned long int iTotalSteps,
120 assert(0!=iTotalSteps);
122 if (rLog.iLogLevelEnabled>LOGLEVEL_THRESHOLD) {
126 (void) snprintf(pcLogMsg, sizeof(pcLogMsg), "%s: %lu %%",
127 prProgress->pcPrefix, (unsigned long int)(iStep/(float)iTotalSteps*100.0));
129 if (! bForceOutput) {
130 /* Skip logging, if we've just logged the same message */
131 if (STR_EQ(pcLogMsg, prProgress->pcLastLogMsg)) {
136 strncpy(prProgress->pcLastLogMsg, pcLogMsg,
137 sizeof(prProgress->pcLastLogMsg));
139 fprintf(prProgress->prFile, "%s (%lu out of %lu)", pcLogMsg, iStep, iTotalSteps);
140 if (prProgress->bPrintCR) {
141 fprintf(prProgress->prFile, "\r");
143 fprintf(prProgress->prFile, "\n");
146 (void) fflush(prProgress->prFile);
150 /*** end: ProgressLog() ***/
154 * @brief Finishes progress output by printing the elapsed time
156 * @param[in] prProgress
157 * Pointer to the progress structure
161 ProgressDone(progress_t *prProgress)
165 if (rLog.iLogLevelEnabled>LOGLEVEL_THRESHOLD) {
169 (void) snprintf(pcBuf, sizeof(pcBuf), "%s done. CPU time: ",
170 prProgress->pcPrefix);
171 StopwatchStop(prProgress->prStopwatch);
172 StopwatchDisplay(prProgress->prFile, pcBuf, prProgress->prStopwatch);
173 (void) fflush(prProgress->prFile);
177 /*** end: ProgressDone() ***/