Wrapper for Clustal Omega.
[jabaws.git] / binaries / src / clustalo / src / clustal / progress.c
1 /* -*- mode: c; tab-width: 4; c-basic-offset: 4;  indent-tabs-mode: nil -*- */
2
3 /*********************************************************************
4  * Clustal Omega - Multiple sequence alignment
5  *
6  * Copyright (C) 2010 University College Dublin
7  *
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.
12  *
13  * This file is part of Clustal-Omega.
14  *
15  ********************************************************************/
16
17 /*
18  *  RCS $Id: progress.c 230 2011-04-09 15:37:50Z andreas $
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <assert.h>
28
29 #include "util.h"
30 #include "log.h"
31 #include "progress.h"
32
33 #define LOGLEVEL_THRESHOLD LOG_INFO
34
35 /**
36  * @brief Allocates a new progress structure and initialises its members.
37  * Free with FreeProgress()
38  *
39  * @note Starts the internal stopwatch immediatly!
40  *
41  * @see FreeProgress()
42  *
43  * @param[out] pprProgress
44  * Pointer pointer to progress structure. Progress structure will be
45  * allocated here. 
46  * @param[in] prFile
47  * Where to log messages to
48  * @param[in] pcPrefix
49  * What prefix to use for messages
50  * @param[in] bPrintCR
51  * If TRUE carriage return instead of newline will be printed between log messages
52  */    
53 void
54 NewProgress(progress_t **pprProgress, FILE *prFile,
55             char *pcPrefix, bool bPrintCR)
56 {
57     assert(NULL!=pprProgress);
58     assert(NULL!=prFile);
59     assert(NULL!=pcPrefix);
60     
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);
69     
70     return; 
71 }
72 /***   end: NewProgress()   ***/
73
74
75
76 /**
77  * @brief Frees progress structure and its members
78  *
79  * @param[out] pprProgress
80  * Pointer pointer to progress structure
81  *
82  * @see NewProgress()
83  * 
84  */    
85 void
86 FreeProgress(progress_t **pprProgress)
87 {
88     (*pprProgress)->prFile = NULL;
89     CKFREE((*pprProgress)->pcPrefix);
90     StopwatchFree((*pprProgress)->prStopwatch);
91
92     CKFREE(*pprProgress);
93     return; 
94 }
95 /***   end: FreeProgress()   ***/
96
97
98
99
100 /**
101  * @brief Prints a progress update (and a carriage return)
102  *
103  * @param[in] prProgress
104  * Pointer to the progress structure
105  * @param[in] iStep
106  * Current step number
107  * @param[in] iTotalSteps
108  * Total step number
109  * @param[in] bForceOutput
110  * If percentage hasn't changed output is normally supressed
111  * normally. Output can be forced with this flag.
112  *
113  */    
114 void
115 ProgressLog(progress_t *prProgress, 
116             unsigned long int iStep, unsigned long int iTotalSteps, 
117             bool bForceOutput)
118 {
119     char pcLogMsg[1024];
120     assert(0!=iTotalSteps);
121     
122     if (rLog.iLogLevelEnabled>LOGLEVEL_THRESHOLD) {
123         return;
124     }
125     
126     (void) snprintf(pcLogMsg, sizeof(pcLogMsg), "%s: %lu %%",
127                     prProgress->pcPrefix, (unsigned long int)(iStep/(float)iTotalSteps*100.0));
128
129     if (! bForceOutput) {
130         /* Skip logging, if we've just logged the same message */
131         if (STR_EQ(pcLogMsg, prProgress->pcLastLogMsg)) {
132             return;
133         }
134     }
135
136     strncpy(prProgress->pcLastLogMsg, pcLogMsg,
137             sizeof(prProgress->pcLastLogMsg));
138     
139     fprintf(prProgress->prFile, "%s (%lu out of %lu)", pcLogMsg, iStep, iTotalSteps);
140     if (prProgress->bPrintCR) {
141         fprintf(prProgress->prFile, "\r");
142     } else {
143         fprintf(prProgress->prFile, "\n");
144
145     }
146     (void) fflush(prProgress->prFile);
147
148     return; 
149 }
150 /***   end: ProgressLog()   ***/
151
152
153 /**
154  * @brief Finishes progress output by printing the elapsed time
155  *
156  * @param[in] prProgress
157  * Pointer to the progress structure
158  *
159  */    
160 void
161 ProgressDone(progress_t *prProgress)
162 {
163     char pcBuf[1024];
164
165     if (rLog.iLogLevelEnabled>LOGLEVEL_THRESHOLD) {
166         return;
167     }
168
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);
174
175     return; 
176 }
177 /***   end: ProgressDone()   ***/