OOFEM 3.0
Loading...
Searching...
No Matches
logger.C
Go to the documentation of this file.
1/*
2 *
3 * ##### ##### ###### ###### ### ###
4 * ## ## ## ## ## ## ## ### ##
5 * ## ## ## ## #### #### ## # ##
6 * ## ## ## ## ## ## ## ##
7 * ## ## ## ## ## ## ## ##
8 * ##### ##### ## ###### ## ##
9 *
10 *
11 * OOFEM : Object Oriented Finite Element Code
12 *
13 * Copyright (C) 1993 - 2025 Borek Patzak
14 *
15 *
16 *
17 * Czech Technical University, Faculty of Civil Engineering,
18 * Department of Structural Mechanics, 166 29 Prague, Czech Republic
19 *
20 * This library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Lesser General Public
22 * License as published by the Free Software Foundation; either
23 * version 2.1 of the License, or (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public
31 * License along with this library; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
33 */
34
35#include "logger.h"
36#include "error.h"
37#include "util.h"
38
39#include <cstdarg>
40#ifdef __MPI_PARALLEL_MODE
41 #include <mpi.h>
42#endif
43
44
45namespace oofem {
46#define LOG_ERR_HEADER "_______________________________________________________"
47#define LOG_ERR_TAIL "_______________________________________________________\a\n"
48
49// Default log output
50Logger oofem_logger(Logger :: LOG_LEVEL_INFO);
51
52Logger :: Logger(logLevelType level) :
53 logStream(stdout),
54 errStream(stderr),
55 closeFlag(false),
56 errCloseFlag(false),
57 logLevel(level),
58 numberOfWrn(0),
60#ifdef __MPI_PARALLEL_MODE
61 ,comm(MPI_COMM_SELF)
62#endif
63{}
64
65Logger :: ~Logger()
66{
67 if ( this->closeFlag ) {
68 fclose(this->logStream);
69 }
70 if ( this->errCloseFlag ) {
71 fclose(this->errStream);
72 }
73}
74
75void
76Logger :: appendLogTo(const std :: string &fname)
77{
78 FILE *stream = NULL;
79 if ( this->closeFlag ) {
80 stream = freopen(fname.c_str(), "a", this->logStream);
81 } else {
82 stream = fopen(fname.c_str(), "a");
83 }
84
85 if ( stream == NULL ) {
86 OOFEM_WARNING( "file opening error (%s)", fname.c_str() );
87 } else {
88 this->logStream = stream;
89 }
90
91 this->closeFlag = true;
92}
93
94void
95Logger :: appendErrorTo(const std :: string &fname)
96{
97 FILE *stream = NULL;
98 if ( this->errCloseFlag ) {
99 stream = freopen(fname.c_str(), "a", this->errStream);
100 } else {
101 stream = fopen(fname.c_str(), "a");
102 }
103
104 if ( stream == NULL ) {
105 OOFEM_WARNING( "file opening error (%s)", fname.c_str() );
106 } else {
107 this->errStream = stream;
108 }
109
110 this->errCloseFlag = true;
111}
112
113void
114Logger :: appendLogTo(FILE *stream)
115{
116 if ( this->closeFlag ) {
117 fclose (this->logStream);
118 }
119
120 if ( stream == NULL ) {
121 OOFEM_ERROR( "Logger::appendLogTo : null stream given" );
122 } else {
123 this->logStream = stream;
124 }
125
126 this->closeFlag = false;
127}
128
129void
130Logger :: appendErrorTo(FILE *stream)
131{
132 if ( this->errCloseFlag ) {
133 fclose (this->errStream);
134 }
135
136 if ( stream == NULL ) {
137 OOFEM_ERROR( "Logger::appendLogTo : null stream given" );
138 } else {
139 this->errStream = stream;
140 }
141
142 this->errCloseFlag = false;
143}
144
145
146
147void
148Logger :: writeLogMsg(logLevelType level, const char *format, ...)
149{
150 int rank = 0;
151
152#ifdef __MPI_PARALLEL_MODE
153 MPI_Comm_rank(this->comm, & rank);
154#endif
155 (void)rank;//prevent a warning about unused variable
156 FILE *stream = this->logStream;
157 if ( level == LOG_LEVEL_FATAL || level == LOG_LEVEL_ERROR ) {
158 numberOfErr++;
159 stream = this->errStream;
160 } else if ( level == LOG_LEVEL_WARNING ) {
161 numberOfWrn++;
162 stream = this->errStream;
163 }
164
165
166 // if ( rank == 0 ) {
167 if (1) {
168 va_list args;
169
170 if ( level <= this->logLevel ) {
171 va_start(args, format);
172 vfprintf(stream, format, args);
173 va_end(args);
174 }
175 }
176}
177
178void
179Logger :: writeELogMsg(logLevelType level, const char *_func, const char *_file, int _line, const char *format, ...)
180{
181 va_list args;
182
183 FILE *stream = this->logStream;
184 if ( level == LOG_LEVEL_FATAL || level == LOG_LEVEL_ERROR ) {
185 numberOfErr++;
186 stream = this->errStream;
187 } else if ( level == LOG_LEVEL_WARNING ) {
188 numberOfWrn++;
189 stream = this->errStream;
190 }
191
192 if ( level <= this->logLevel ) {
193 if ( _file ) {
194 fprintf(stream, "%s\n%s:", LOG_ERR_HEADER, giveLevelName(level));
195 } else {
196 fprintf(stream, "%s\n%s:", LOG_ERR_HEADER, giveLevelName(level));
197 }
198
199 va_start(args, format);
200 vfprintf(stream, format, args);
201 va_end(args);
202 fprintf(stream, "\n");
203
204 if ( _func ) {
205 fprintf(stream, "In %s ", _func );
206 }
207
208 if ( _file ) {
209 fprintf(stream, "(%s:%d)", _file, _line);
210 }
211
212 fprintf(stream, "\n%s", LOG_ERR_TAIL);
213 }
214
215 if ( level == LOG_LEVEL_FATAL || level == LOG_LEVEL_ERROR ) {
216#ifndef CEMPY
217 print_stacktrace(this->errStream, 10);
218#endif
219 }
220}
221
222const char *
223Logger :: giveLevelName(logLevelType l) const
224{
225 switch ( l ) {
226 //case LOG_LEVEL_FATAL:
227 case LOG_LEVEL_ERROR:
228 return "Error";
229
231 return "Warning";
232
233 default:
234 return "Info";
235 }
236}
237
238void
239Logger :: setLogLevel(int level)
240{
241 if ( ( level >= ( int ) LOG_LEVEL_FATAL ) && ( level <= ( int ) LOG_LEVEL_DEBUG ) ) {
242 this->logLevel = ( logLevelType ) level;
243 }
244}
245
246#ifdef __MPI_PARALLEL_MODE
247void
248Logger :: setComm(MPI_Comm comm)
249{
250 this->comm = comm;
251}
252#endif
253
254void
255Logger :: printStatistics()
256{
257 int rank = 0;
258
259#ifdef __MPI_PARALLEL_MODE
260 MPI_Comm_rank(this->comm, & rank);
261#endif
262
263 int totalNumberOfErr = numberOfErr, totalNumberOfWrn = numberOfWrn;
264#ifdef __MPI_PARALLEL_MODE
265 MPI_Reduce(& numberOfErr, & totalNumberOfErr, 1, MPI_INT, MPI_SUM, 0, this->comm);
266 MPI_Reduce(& numberOfWrn, & totalNumberOfWrn, 1, MPI_INT, MPI_SUM, 0, this->comm);
267#endif
268 if ( rank == 0 ) {
269 // force output
270 fprintf(logStream, "Total %d error(s) and %d warning(s) reported\n", totalNumberOfErr, totalNumberOfWrn);
271 }
272}
273
274} // end namespace oofem
bool closeFlag
flag indicating whether to close mylogStream.
Definition logger.h:73
const char * giveLevelName(logLevelType l) const
Definition logger.C:223
logLevelType
Type defining basic log levels.
Definition logger.h:61
@ LOG_LEVEL_ERROR
Definition logger.h:63
@ LOG_LEVEL_FATAL
Definition logger.h:63
@ LOG_LEVEL_DEBUG
Definition logger.h:67
@ LOG_LEVEL_WARNING
Definition logger.h:64
int numberOfWrn
Counter of all warning and error messages.
Definition logger.h:77
int numberOfErr
Definition logger.h:77
MPI_Comm comm
Parallell comm.
Definition logger.h:80
bool errCloseFlag
Definition logger.h:73
FILE * errStream
Definition logger.h:71
logLevelType logLevel
Current log level, messages with higher level are not reported.
Definition logger.h:75
FILE * logStream
Stream used for logging.
Definition logger.h:71
#define OOFEM_WARNING(...)
Definition error.h:80
#define OOFEM_ERROR(...)
Definition error.h:79
#define LOG_ERR_TAIL
Definition logger.C:47
#define LOG_ERR_HEADER
Definition logger.C:46
Logger oofem_logger(Logger ::LOG_LEVEL_INFO)
Definition logger.h:131
void print_stacktrace(FILE *out, int skip, unsigned int max_frames)
Definition util.C:63

This page is part of the OOFEM-3.0 documentation. Copyright Copyright (C) 1994-2025 Borek Patzak Bořek Patzák
Project e-mail: oofem@fsv.cvut.cz
Generated at for OOFEM by doxygen 1.15.0 written by Dimitri van Heesch, © 1997-2011