BELFEM 0.9.0
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
cl_Logger.hpp
Go to the documentation of this file.
1/*
2 * BELFEM -- The Berkeley Lab Finite Element Framework
3 * Copyright (c) 2026, The Regents of the University of California,
4 * through Lawrence Berkeley National Laboratory (subject to receipt of any required
5 * approvals from the U.S. Dept. of Energy). All rights reserved.
6 *
7 * Developers: Christian Messe, Gregory Giard
8 *
9 * See the top-level LICENSE file for the complete license and disclaimer.
10 */
11
12#ifndef BELFEM_CL_LOGGER_HPP
13#define BELFEM_CL_LOGGER_HPP
14
15#include <cstdio>
16#include <fstream>
17
18#include "stringtools.hpp"
19#include "typedefs.hpp"
20
21#ifdef BELFEM_CLANG
22#pragma clang diagnostic push
23#pragma clang diagnostic ignored "-Wformat-security"
24#elif BELFEM_GCC
25#pragma GCC diagnostic push
26#pragma GCC diagnostic ignored "-Wformat"
27#endif
28
29namespace belfem
30{
31 enum class InfoLevel
32 {
33 Silent = 0, // nothing
34 Minimal = 1, // minimal output
35 Default = 2, // default output
36 Detailed = 3, // a little more info about the mesh
37 Verbose = 4, // BELEM specific debugging info
38 Everything = 5 // also print debugging info from third party libraries
39 };
40
41//------------------------------------------------------------------------------
42
49 class Logger
50 {
51//------------------------------------------------------------------------------
52
53 uint mInfoLevel = 0 ;
54 std::FILE* mStream;
55 bool mWriteToAscii = false;
56
57 // non-copyable, non-movable ( owns the FILE handle in ASCII mode )
58 Logger( const Logger & ) = delete ;
59 Logger & operator=( const Logger & ) = delete ;
60 Logger( Logger && ) = delete ;
61 Logger & operator=( Logger && ) = delete ;
62
63//------------------------------------------------------------------------------
64 public:
65//------------------------------------------------------------------------------
66
70 Logger( const uint aInfoLevel );
71
75 Logger( const InfoLevel aInfoLevel );
76
77//------------------------------------------------------------------------------
78
82 Logger( const InfoLevel aInfoLevel, const std::string & aPath );
83
84//------------------------------------------------------------------------------
85
89 ~Logger();
90
91//------------------------------------------------------------------------------
92
96 uint
97 info_level() const;
98
99//------------------------------------------------------------------------------
100
104 void
105 set_info_level( const uint aInfoLevel );
106
107 void
108 set_info_level( const InfoLevel aInfoLevel );
109
110//------------------------------------------------------------------------------
111
112 template < typename ... Args >
113 void
115 const InfoLevel aInfoLevel,
116 const std::string & aFormat,
117 const Args ... aArgs )
118 {
119 if( static_cast< uint > ( aInfoLevel ) <= mInfoLevel )
120 {
121 // format message and append a new line
122 std::string tMessage = sprint(
123 aFormat.c_str(),
124 aArgs ... ) + "\n";
125
126 std::fprintf( mStream, tMessage.c_str() );
127 }
128 }
129
130//------------------------------------------------------------------------------
131 };
132
133//------------------------------------------------------------------------------
134}
135 // Externally Defined Global Logger
136 extern belfem::Logger gLog;
137
138//------------------------------------------------------------------------------
139
140
141
142 template < typename ... Args >
143 void
145 const belfem::InfoLevel aInfoLevel,
146 const std::string & aFormat,
147 const Args ... aArgs )
148 {
149 gLog.message( aInfoLevel, aFormat, aArgs ... );
150 }
151
152#ifdef BELFEM_CLANG
153#pragma clang diagnostic pop
154#elif BELFEM_GCC
155#pragma GCC diagnostic pop
156#endif
157
158//------------------------------------------------------------------------------
159
160#endif //BELFEM_CL_LOGGER_HPP
belfem::Logger gLog
void message(const belfem::InfoLevel aInfoLevel, const std::string &aFormat, const Args ... aArgs)
Definition cl_Logger.hpp:144
Hierarchical logging with info levels.
Definition cl_Logger.hpp:50
void message(const InfoLevel aInfoLevel, const std::string &aFormat, const Args ... aArgs)
Definition cl_Logger.hpp:114
void set_info_level(const uint aInfoLevel)
set the info level at runtime, e.g.
Definition cl_Logger.cpp:71
uint info_level() const
return the info level of the logger
Definition cl_Logger.cpp:63
USER GUIDES:
Definition cl_Capacitor.cpp:16
unsigned int uint
Definition typedefs.hpp:30
std::string sprint(const char *aFormat, const Args ... aArgs)
A format script similar to write( , ) in fortran.
Definition fn_sprint.hpp:44
InfoLevel
Definition cl_Logger.hpp:32
@ Silent
Definition cl_Logger.hpp:33
@ Minimal
Definition cl_Logger.hpp:34
@ Detailed
Definition cl_Logger.hpp:36
@ Everything
Definition cl_Logger.hpp:38
@ Default
Definition cl_Logger.hpp:35
@ Verbose
Definition cl_Logger.hpp:37