Jetson Inference
DNN Vision Library
logging.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __LOGGING_UTILS_H_
24 #define __LOGGING_UTILS_H_
25 
26 #include "commandLine.h"
27 
28 #include <stdio.h>
29 #include <string>
30 
31 
36 #define LOG_USAGE_STRING "logging arguments: \n" \
37  " --log-file=FILE output destination file (default is stdout)\n" \
38  " --log-level=LEVEL message output threshold, one of the following:\n" \
39  " * silent\n" \
40  " * error\n" \
41  " * warning\n" \
42  " * success\n" \
43  " * info\n" \
44  " * verbose (default)\n" \
45  " * debug\n" \
46  " --verbose enable verbose logging (same as --log-level=verbose)\n" \
47  " --debug enable debug logging (same as --log-level=debug)\n\n"
48 
49 
54 class Log
55 {
56 public:
61  enum Level
62  {
63  SILENT=0,
67  INFO,
71  };
72 
76  static inline Level GetLevel() { return mLevel; }
77 
81  static inline void SetLevel( Level level ) { mLevel = level; }
82 
86  static inline FILE* GetFile() { return mFile; }
87 
92  static inline const char* GetFilename() { return mFilename.c_str(); }
93 
99  static void SetFile( FILE* file );
100 
105  static void SetFile( const char* filename );
106 
110  static inline const char* Usage() { return LOG_USAGE_STRING; }
111 
115  static void ParseCmdLine( const int argc, char** argv );
116 
120  static void ParseCmdLine( const commandLine& cmdLine );
121 
125  static const char* LevelToStr( Level level );
126 
130  static Level LevelFromStr( const char* str );
131 
132 protected:
133  static Level mLevel;
134  static FILE* mFile;
135  static std::string mFilename;
136 };
137 
138 
144 #define GenericLogMessage(level, format, args...) if( level <= Log::GetLevel() ) fprintf(Log::GetFile(), format, ## args)
145 
150 #define LogError(format, args...) GenericLogMessage(Log::ERROR, LOG_COLOR_RED LOG_LEVEL_PREFIX_ERROR format LOG_COLOR_RESET, ## args)
151 
156 #define LogWarning(format, args...) GenericLogMessage(Log::WARNING, LOG_COLOR_YELLOW LOG_LEVEL_PREFIX_WARNING format LOG_COLOR_RESET, ## args)
157 
162 #define LogSuccess(format, args...) GenericLogMessage(Log::SUCCESS, LOG_COLOR_GREEN LOG_LEVEL_PREFIX_SUCCESS format LOG_COLOR_RESET, ## args)
163 
168 #define LogInfo(format, args...) GenericLogMessage(Log::INFO, LOG_LEVEL_PREFIX_INFO format, ## args)
169 
174 #define LogVerbose(format, args...) GenericLogMessage(Log::VERBOSE, LOG_LEVEL_PREFIX_VERBOSE format, ## args)
175 
180 #define LogDebug(format, args...) GenericLogMessage(Log::DEBUG, LOG_LEVEL_PREFIX_DEBUG format, ## args)
181 
182 
188 
190 
191 #ifdef LOG_DISABLE_COLORS
192  #define LOG_COLOR_RESET ""
193  #define LOG_COLOR_RED ""
194  #define LOG_COLOR_GREEN ""
195  #define LOG_COLOR_YELLOW ""
196  #define LOG_COLOR_BLUE ""
197  #define LOG_COLOR_MAGENTA ""
198  #define LOG_COLOR_CYAN ""
199  #define LOG_COLOR_LIGHT_GRAY ""
200  #define LOG_COLOR_DARK_GRAY ""
201 #else
202  // https://misc.flogisoft.com/bash/tip_colors_and_formatting
203  #define LOG_COLOR_RESET "\033[0m"
204  #define LOG_COLOR_RED "\033[0;31m"
205  #define LOG_COLOR_GREEN "\033[0;32m"
206  #define LOG_COLOR_YELLOW "\033[0;33m"
207  #define LOG_COLOR_BLUE "\033[0;34m"
208  #define LOG_COLOR_MAGENTA "\033[0;35m"
209  #define LOG_COLOR_CYAN "\033[0;36m"
210  #define LOG_COLOR_LIGHT_GRAY "\033[0;37m"
211  #define LOG_COLOR_DARK_GRAY "\033[0;90m"
212 #endif
213 
214 #ifdef LOG_ENABLE_LEVEL_PREFIX
215  #define LOG_LEVEL_PREFIX_ERROR "[E]"
216  #define LOG_LEVEL_PREFIX_WARNING "[W]"
217  #define LOG_LEVEL_PREFIX_SUCCESS "[S]"
218  #define LOG_LEVEL_PREFIX_INFO "[I]"
219  #define LOG_LEVEL_PREFIX_VERBOSE "[V]"
220  #define LOG_LEVEL_PREFIX_DEBUG "[D]"
221 #else
222  #define LOG_LEVEL_PREFIX_ERROR ""
223  #define LOG_LEVEL_PREFIX_WARNING ""
224  #define LOG_LEVEL_PREFIX_SUCCESS ""
225  #define LOG_LEVEL_PREFIX_INFO ""
226  #define LOG_LEVEL_PREFIX_VERBOSE ""
227  #define LOG_LEVEL_PREFIX_DEBUG ""
228 #endif
229 
231 
232 #endif
233 
Log::Usage
static const char * Usage()
Usage string for command line arguments to Create()
Definition: logging.h:110
Log::Level
Level
Defines the logging level of a message, and the threshold used by the logger to either drop or output...
Definition: logging.h:61
Log::LevelToStr
static const char * LevelToStr(Level level)
Convert a logging level to string.
Log::DEFAULT
@ DEFAULT
The default level is VERBOSE
Definition: logging.h:70
Log::SetFile
static void SetFile(FILE *file)
Set the logging output.
commandLine.h
Log::SetLevel
static void SetLevel(Level level)
Set the current logging level.
Definition: logging.h:81
Log::WARNING
@ WARNING
Warning conditions where the application may be able to proceed in some capacity.
Definition: logging.h:65
Log::VERBOSE
@ VERBOSE
Verbose details about program execution.
Definition: logging.h:68
Log::SILENT
@ SILENT
No messages are output.
Definition: logging.h:63
Log::mLevel
static Level mLevel
Definition: logging.h:133
Log::GetFilename
static const char * GetFilename()
Get the filename of the log output.
Definition: logging.h:92
Log::LevelFromStr
static Level LevelFromStr(const char *str)
Parse a logging level from a string.
Log::GetLevel
static Level GetLevel()
Get the current logging level.
Definition: logging.h:76
Log::SUCCESS
@ SUCCESS
Successful events (e.g.
Definition: logging.h:66
Log::mFilename
static std::string mFilename
Definition: logging.h:135
Log
Message logging with a variable level of output and destinations.
Definition: logging.h:54
Log::mFile
static FILE * mFile
Definition: logging.h:134
LOG_USAGE_STRING
#define LOG_USAGE_STRING
Standard command-line options able to be passed to videoOutput::Create()
Definition: logging.h:36
Log::INFO
@ INFO
Informational messages that are more important than VERBOSE messages.
Definition: logging.h:67
Log::ParseCmdLine
static void ParseCmdLine(const int argc, char **argv)
Parse command line options (see Usage() above)
commandLine
Command line parser for extracting flags, values, and strings.
Definition: commandLine.h:35
Log::ERROR
@ ERROR
Major errors that may impact application execution.
Definition: logging.h:64
Log::GetFile
static FILE * GetFile()
Get the current log output.
Definition: logging.h:86
Log::DEBUG
@ DEBUG
Low-level debugging (disabled by default)
Definition: logging.h:69