μHAL (v2.7.9)
Part of the IPbus software repository
test_log.cxx
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23  Marc Magrans de Abril, CERN
24  email: marc.magrans.de.abril <AT> cern.ch
25 
26  Andrew Rose, Imperial College, London
27  email: awr01 <AT> imperial.ac.uk
28 
29 ---------------------------------------------------------------------------
30 */
31 
39 #include <iostream>
40 
41 #include "uhal/log/log.hpp"
42 
43 #include "uhal/utilities/files.hpp"
44 
45 
46 using namespace uhal;
47 
48 template< typename T >
49 struct TestStruct
50 {
51  T mA;
52  T mB;
53  T mC;
54  T mD;
55 };
56 
57 
58 int main ( int argc,char* argv[] )
59 {
60  try
61  {
62  // Test basic inserters
63  log ( Notice() , "const char*" );
64  log ( Notice() , std::string ( "std::string" ) );
65  log ( Notice() , 'c' );
66  log ( Notice() , "a" , 'b' , std::string ( "c" ) , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z" , "0" , "1" , "2" , "3" , "4" , "5" );
67  timeval lTime;
68  gettimeofday ( &lTime, NULL );
69  log ( Notice() , Time ( lTime ) , " default formatting" );
70  log ( Notice() , Time ( lTime , TimeFmt< day,'/',mth,'/',year,' ',hr,':',min,':',sec,'.',usec >() ) , " truncated to the nearest usecond" );
71  log ( Notice() , Time ( lTime , TimeFmt< day,'/',mth,'/',year,' ',hr,':',min,':',sec >() ) , " truncated to the nearest second" );
72  log ( Notice() , Time ( lTime , TimeFmt< day,'/',mth,'/',year,' ',hr,':',min >() ) , " truncated to the nearest minute" );
73  log ( Notice() , Time ( lTime , TimeFmt< day,'/',mth,'/',year,' ',hr >() ) , " truncated to the nearest hour" );
74  log ( Notice() , Time ( lTime , TimeFmt< day,'/',mth,'/',year >() ) , " truncated to the nearest day" );
75  log ( Notice() , Time ( lTime , TimeFmt< year,'/',mth,'/',day >() ) , " truncated to the nearest day (US format)" );
76  log ( Notice() , Time ( lTime , TimeFmt< mth,'/',year >() ) , " truncated to the nearest month" );
77  log ( Notice() , Time ( lTime , TimeFmt< strmth,' ',year >() ) , " truncated to the nearest month" );
78  log ( Notice() , Time ( lTime , TimeFmt< year >() ) , " truncated to the nearest year (4 digit format)" );
79  log ( Notice() , Time ( lTime , TimeFmt< yr >() ) , " truncated to the nearest year (2 digit format)" );
80  log ( Notice() , Time ( lTime , TimeFmt< usec >() ) , " microseconds" );
81  log ( Notice() , "13 as a decimal uint8 : " , Integer ( uint8_t ( 13 ) ) );
82  log ( Notice() , "-13 as a decimal int8 : " , Integer ( int8_t ( -13 ) ) );
83  log ( Notice() , "13 as a decimal uint16 : " , Integer ( uint16_t ( 13 ) ) );
84  log ( Notice() , "-13 as a decimal int16 : " , Integer ( int16_t ( -13 ) ) );
85  log ( Notice() , "13 as a decimal uint32 : " , Integer ( uint32_t ( 13 ) ) );
86  log ( Notice() , "-13 as a decimal int32 : " , Integer ( int32_t ( -13 ) ) );
87  log ( Notice() , "13 as a decimal uint64 : " , Integer ( uint64_t ( 13 ) ) );
88  log ( Notice() , "-13 as a decimal int64 : " , Integer ( int64_t ( -13 ) ) );
89  log ( Notice() , "13 as a variable-width binary uint8 : " , Integer ( uint8_t ( 13 ) , IntFmt<bin>() ) );
90  log ( Notice() , "-13 as a variable-width binary int8 : " , Integer ( int8_t ( -13 ) , IntFmt<bin>() ) );
91  log ( Notice() , "13 as a variable-width binary uint16 : " , Integer ( uint16_t ( 13 ) , IntFmt<bin>() ) );
92  log ( Notice() , "-13 as a variable-width binary int16 : " , Integer ( int16_t ( -13 ) , IntFmt<bin>() ) );
93  log ( Notice() , "13 as a variable-width binary uint32 : " , Integer ( uint32_t ( 13 ) , IntFmt<bin>() ) );
94  log ( Notice() , "-13 as a variable-width binary int32 : " , Integer ( int32_t ( -13 ) , IntFmt<bin>() ) );
95  log ( Notice() , "13 as a variable-width binary uint64 : " , Integer ( uint64_t ( 13 ) , IntFmt<bin>() ) );
96  log ( Notice() , "-13 as a variable-width binary int64 : " , Integer ( int64_t ( -13 ) , IntFmt<bin>() ) );
97  log ( Notice() , "13 as a variable-width hex uint8 : " , Integer ( uint8_t ( 13 ) , IntFmt<hex>() ) );
98  log ( Notice() , "-13 as a variable-width hex int8 : " , Integer ( int8_t ( -13 ) , IntFmt<hex>() ) );
99  log ( Notice() , "13 as a variable-width hex uint16 : " , Integer ( uint16_t ( 13 ) , IntFmt<hex>() ) );
100  log ( Notice() , "-13 as a variable-width hex int16 : " , Integer ( int16_t ( -13 ) , IntFmt<hex>() ) );
101  log ( Notice() , "13 as a variable-width hex uint32 : " , Integer ( uint32_t ( 13 ) , IntFmt<hex>() ) );
102  log ( Notice() , "-13 as a variable-width hex int32 : " , Integer ( int32_t ( -13 ) , IntFmt<hex>() ) );
103  log ( Notice() , "13 as a variable-width hex uint64 : " , Integer ( uint64_t ( 13 ) , IntFmt<hex>() ) );
104  log ( Notice() , "-13 as a variable-width hex int64 : " , Integer ( int64_t ( -13 ) , IntFmt<hex>() ) );
105  log ( Notice() , "13 as a fixed-width binary uint8 : " , Integer ( uint8_t ( 13 ) , IntFmt<bin , fixed>() ) );
106  log ( Notice() , "-13 as a fixed-width binary int8 : " , Integer ( int8_t ( -13 ) , IntFmt<bin , fixed>() ) );
107  log ( Notice() , "13 as a fixed-width binary uint16 : " , Integer ( uint16_t ( 13 ) , IntFmt<bin , fixed>() ) );
108  log ( Notice() , "-13 as a fixed-width binary int16 : " , Integer ( int16_t ( -13 ) , IntFmt<bin , fixed>() ) );
109  log ( Notice() , "13 as a fixed-width binary uint32 : " , Integer ( uint32_t ( 13 ) , IntFmt<bin , fixed>() ) );
110  log ( Notice() , "-13 as a fixed-width binary int32 : " , Integer ( int32_t ( -13 ) , IntFmt<bin , fixed>() ) );
111  log ( Notice() , "13 as a fixed-width binary uint64 : " , Integer ( uint64_t ( 13 ) , IntFmt<bin , fixed>() ) );
112  log ( Notice() , "-13 as a fixed-width binary int64 : " , Integer ( int64_t ( -13 ) , IntFmt<bin , fixed>() ) );
113  log ( Notice() , "13 as a fixed-width hex uint8 : " , Integer ( uint8_t ( 13 ) , IntFmt<hex , fixed>() ) );
114  log ( Notice() , "-13 as a fixed-width hex int8 : " , Integer ( int8_t ( -13 ) , IntFmt<hex , fixed>() ) );
115  log ( Notice() , "13 as a fixed-width hex uint16 : " , Integer ( uint16_t ( 13 ) , IntFmt<hex , fixed>() ) );
116  log ( Notice() , "-13 as a fixed-width hex int16 : " , Integer ( int16_t ( -13 ) , IntFmt<hex , fixed>() ) );
117  log ( Notice() , "13 as a fixed-width hex uint32 : " , Integer ( uint32_t ( 13 ) , IntFmt<hex , fixed>() ) );
118  log ( Notice() , "-13 as a fixed-width hex int32 : " , Integer ( int32_t ( -13 ) , IntFmt<hex , fixed>() ) );
119  log ( Notice() , "13 as a fixed-width hex uint64 : " , Integer ( uint64_t ( 13 ) , IntFmt<hex , fixed>() ) );
120  log ( Notice() , "-13 as a fixed-width hex int64 : " , Integer ( int64_t ( -13 ) , IntFmt<hex , fixed>() ) );
121  log ( Notice() , "Specified fixed width bin uint8_t : " , Integer ( uint8_t ( 13 ) , IntFmt<bin , fixed , 10>() ) );
122  log ( Notice() , "Specified fixed width dec uint8_t : " , Integer ( uint8_t ( 13 ) , IntFmt<dec , fixed , 10>() ) );
123  log ( Notice() , "Specified fixed width hex uint8_t : " , Integer ( uint8_t ( 13 ) , IntFmt<hex , fixed , 10>() ) );
124  // log ( Notice() , "double pi : " , Real ( double ( 3.1415926535 ) ) );
125  // log ( Notice() , "float pi : " , Real ( float ( 3.1415926535 ) ) , " (should be truncated)" );
126  // log ( Notice() , "double pi : " , Real ( double ( 3.1415926535 ) , RealFmt<4>() ) );
127  // log ( Notice() , "float pi : " , Real ( float ( 3.1415926535 ) , RealFmt<4>() ) , " (should be truncated)" );
128  // log ( Notice() , "double pi x 10^15 : " , Real ( double ( 3.1415926535e15 ) ) );
129  // log ( Notice() , "float pi x 10^15 : " , Real ( float ( 3.1415926535e15 ) ) , " (should be truncated)" );
130  // log ( Notice() , "double pi x 10^-2 : " , Real ( double ( 3.1415926535e-2 ) ) );
131  // log ( Notice() , "float pi x 10^-2 : " , Real ( float ( 3.1415926535e-2 ) ) , " (should be truncated)" );
132  // log ( Notice() , "Boolean true : " , Boolean ( true ) );
133  // log ( Notice() , "Boolean false : " , Boolean ( false ) );
134  // log ( Notice() , "Boolean true : " , Boolean ( true , BoolFmt<alpha>() ) );
135  // log ( Notice() , "Boolean false : " , Boolean ( false , BoolFmt<alpha>() ) );
136  // log ( Notice() , "Boolean true : " , Boolean ( true , BoolFmt<numeric>() ) );
137  // log ( Notice() , "Boolean false : " , Boolean ( false , BoolFmt<numeric>() ) );
138  // TestStruct<uint32_t> lTest = { 0x00112233 , 0x44556677 , 0x8899AABB , 0xCCDDEEFF };
139  // log ( Notice() , "Hex dump (struct) : " , Hex ( lTest ) );
140  // log ( Notice() , "Hex dump (one of the logging wrappers) : " , Hex ( Integer ( uint64_t ( 13 ) ) ) );
141  // log ( Notice() , "Hex dump (POD) : " , Hex ( uint64_t ( 13 ) ) );
142  // log ( Notice() , "Pointer : " , Pointer ( &lTest ) );
143  // log ( Notice() , "Pointer : " , Pointer ( argv ) );
144  // log ( Notice() , "Pointer : " , Pointer ( *argv ) );
145  log ( Notice() , "Quote : " , Quote ( Type< TestStruct<uint32_t> >() ) );
146  log ( Notice() , "Specified fixed width hex uint8_t : " , Integer ( uint8_t ( 13 ) , IntFmt<hex , fixed , 10>() ) );
147  exception::FileNotFound lExc;
148  log ( lExc , "Specified fixed width hex uint8_t : " , Integer ( uint8_t ( 13 ) , IntFmt<hex , fixed , 10>() ) );
149  log ( lExc , "Specified fixed width hex uint8_t : " , Integer ( uint8_t ( 13 ) , IntFmt<hex , fixed , 10>() ) );
150  log ( lExc , "Specified fixed width hex uint8_t : " , Integer ( uint8_t ( 13 ) , IntFmt<hex , fixed , 10>() ) );
151  std::cout << lExc.what() << std::endl;
152  // Test setting logging levels
153  setLogLevelTo ( Error() );
154  log ( Notice() , std::string ( "setLogLevelTo ( Error() ) : IF YOU >>DO<< SEE THIS, THEN THERE IS AN ERROR" ) );
155  log ( Error() , std::string ( "setLogLevelTo ( Error() ) : IF YOU DON'T SEE THIS, THEN THERE IS AN ERROR" ) );
156  setLogLevelTo ( Notice() );
157  log ( Notice() , std::string ( "setLogLevelTo ( Notice() ) : IF YOU DON'T SEE THIS, THEN THERE IS AN ERROR" ) );
158  log ( Error() , std::string ( "setLogLevelTo ( Notice() ) : IF YOU DON'T SEE THIS, THEN THERE IS AN ERROR" ) );
159  // log ( Notice() , "Dodgy cast : " , Integer ( bool(true) , IntFmt<hex , fixed>() ) );
160  // log ( Notice() , "Dodgy cast : " , Boolean ( uint32_t(13) ) );
161  setLogLevelFromEnvironment ( "UHAL_LOG_LEVEL" );
162  log ( Notice() , std::string ( "setLogLevelFromEnvironment ( \"UHAL_LOG_LEVEL\" ) : IF \"UHAL_LOG_LEVEL\" IS DEFINED ABOVE \"Notice()\" AND YOU >>DO<< SEE THIS, THEN THERE IS AN ERROR" ) );
163  log ( Error() , std::string ( "setLogLevelFromEnvironment ( \"UHAL_LOG_LEVEL\" ) : IF \"UHAL_LOG_LEVEL\" IS NOT DEFINED ABOVE \"Error()\" AND YOU DON'T SEE THIS, THEN THERE IS AN ERROR" ) );
164  disableLogging();
165  log ( Notice() , std::string ( "disableLogging() : IF YOU >>DO<< SEE THIS, THEN THERE IS AN ERROR" ) );
166  log ( Error() , std::string ( "disableLogging() : IF YOU >>DO<< SEE THIS, THEN THERE IS AN ERROR" ) );
167  //Set log back to notice
168  setLogLevelTo ( Notice() );
169  log ( Notice() , std::string ( "setLogLevelTo ( Notice() ) : IF YOU DON'T SEE THIS, THEN THERE IS AN ERROR" ) );
170  log ( Error() , std::string ( "setLogLevelTo ( Notice() ) : IF YOU DON'T SEE THIS, THEN THERE IS AN ERROR" ) );
171  }
172  catch ( const std::exception& aExc )
173  {
174  std::cerr << "ERROR: Caught Exception : " << aExc.what() << std::endl;
175  exit ( 1 );
176  }
177 }
178 
179 
uhal::Type
_Type< T > Type()
Definition: log_inserters.type.hxx:60
main
int main(int argc, char *argv[])
Definition: test_log.cxx:58
TestStruct::mC
T mC
Definition: test_log.cxx:53
uhal::IntFmt
Empty struct which acts as a dummy variable for passing the formatting information around.
Definition: log_inserters.integer.hpp:68
uhal::setLogLevelTo
void setLogLevelTo(const FatalLevel &)
Function to specify, at runtime, that only messages with a severity level above Fatal should be logge...
Definition: log.cpp:88
uhal
Definition: HttpResponseGrammar.hpp:49
files.hpp
TestStruct
Definition: test_log.cxx:50
uhal::log
void log(FatalLevel &aFatal, const T0 &aArg0)
Function to add a log entry at Fatal level.
Definition: log.hxx:20
uhal::TimeFmt
A struct whose template parameters represent a time format.
Definition: log_inserters.time.hpp:75
uhal::Integer
_Integer< T, IntFmt<> > Integer(const T &aT)
Forward declare a function which creates an instance of the ultra-lightweight wrapper from an integer...
Definition: log_inserters.integer.hxx:43
TestStruct::mD
T mD
Definition: test_log.cxx:54
uhal::disableLogging
void disableLogging()
Function to disable all logging levels.
Definition: log.cpp:65
uhal::Error
ErrorLevel Error
Definition: LogLevels.cpp:61
uhal::Quote
_Quote< T > Quote(const T &aT)
Definition: log_inserters.quote.hxx:49
TestStruct::mB
T mB
Definition: test_log.cxx:52
uhal::Time
_Time< DefaultTimeFmt > Time(const timeval &aTime)
Helper function which wrap the template uglyness in a pretty package.
Definition: log_inserters.time.hxx:53
uhal::Notice
NoticeLevel Notice
Definition: LogLevels.cpp:97
log.hpp
TestStruct::mA
T mA
Definition: test_log.cxx:51
uhal::setLogLevelFromEnvironment
void setLogLevelFromEnvironment(const char *aEnvVar)
Function to specify that the logging level should be retrieved from an environment variable.
Definition: log.cpp:17