CG_Labs  2021.2
Log.h
Go to the documentation of this file.
1 /*
2  * Error and warning handling system
3  */
4 
5 #include "BuildSettings.h"
6 
7 #include <cstddef>
8 #include <cstdarg>
9 
10 #pragma once
11 //#define ENABLE_ASSERT 1
12 //#define ENABLE_PARAM_CHECK 1
13 
14 #define SUCCESS(r) ((r) == RESULT_SUCCESS)
15 #define FAILURE(r) (!SUCCESS(r))
16 
17 #if defined ENABLE_ASSERT && ENABLE_ASSERT != 0
18 # define Assert(m) if (!(static_cast<unsigned int>(m))) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_ASSERT, "Assertion failed at line %u in file %s.", __LINE__, __FILE__)
19 #else
20 # define Assert(a)
21 #endif
22 
23 #if defined ENABLE_PARAM_CHECK && ENABLE_PARAM_CHECK != 0
24 # define Param(m) Log::ReportParam(static_cast<unsigned int>(m), __FILE__, __FUNCTION__, __LINE__)
25 #else
26 # define Param(a) true
27 #endif
28 
29 #define LOG_MESSAGE_ONCE_FLAG 1
30 #define LOG_LOCATION_ONCE_FLAG 2
31 
32 namespace Log {
33 
34 enum Type {
36  TYPE_INFO = 1,
40  TYPE_FILE = 5,
44  N_TYPES = 9
45 };
46 
47 enum Severity {
48  OK = 0,
49  BAD,
50  TERMINAL
51 };
52 enum Verbosity {
53  WHISPER = 0, // Disregard message
54  LOUD_UNSITUATED, // Display message
55  LOUD // Display message, with file, function and line prepended
56 };
57 
58 #define LOG_OUT_STD (1 << 0)
59 #define LOG_OUT_FILE (1 << 1)
60 #define LOG_OUT_CUSTOM (1 << 15)
61 
62 void Init();
63 void Destroy();
64 void SetCustomOutputTargetFunc(void (* textout)(Type, const char *));
65 void SetOutputTargets(std::size_t targets);
66 void SetVerbosity(Type type, Verbosity verbosity);
67 void SetIncludeThreadID(bool inc);
68 
70 void Report(
71  unsigned int flags,
72  const char *file,
73  const char *function,
74  int line,
75  Type type,
76  const char *str,
77  ...
78  );
79 
80 bool ReportParam(
81  unsigned int test,
82  const char *file,
83  const char *function,
84  int line
85  );
86 
87 };
88 
89 //__forceinline Log::Type operator|(Log::Type l, Log::Type r)
90 //{
91 // return static_cast<Log::Type>(static_cast<int>(l) | static_cast<int>(r));
92 //}
93 
94 #if defined _WIN32
95 # define Log(m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_NEUTRAL, static_cast<char const*>(m), __VA_ARGS__)
96 # define LogType(t, m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, t, static_cast<char const*>(m), __VA_ARGS__)
97 # define LogMsgOnce(t, m, ...) Log::Report(LOG_MESSAGE_ONCE_FLAG, __FILE__, __FUNCTION__, __LINE__, t, static_cast<char const*>(m), __VA_ARGS__)
98 # define LogLocOnce(t, m, ...) Log::Report(LOG_LOCATION_ONCE_FLAG, __FILE__, __FUNCTION__, __LINE__, t, static_cast<char const*>(m), __VA_ARGS__)
99 # define LogWarning(m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_WARNING, static_cast<char const*>(m), __VA_ARGS__)
100 # define LogError(m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_ERROR, static_cast<char const*>(m), __VA_ARGS__)
101 # define LogFile(m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_FILE, static_cast<char const*>(m), __VA_ARGS__)
102 # define LogInfo(m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_INFO, static_cast<char const*>(m), __VA_ARGS__)
103 # define LogTrivia(m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_TRIVIA, static_cast<char const*>(m), __VA_ARGS__)
104 #else
105 # define Log(m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_NEUTRAL, static_cast<char const*>(m), ##__VA_ARGS__)
106 # define LogType(t, m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, t, static_cast<char const*>(m), ##__VA_ARGS__)
107 # define LogMsgOnce(t, m, ...) Log::Report(LOG_MESSAGE_ONCE_FLAG, __FILE__, __FUNCTION__, __LINE__, t, static_cast<char const*>(m), ##__VA_ARGS__)
108 # define LogLocOnce(t, m, ...) Log::Report(LOG_LOCATION_ONCE_FLAG, __FILE__, __FUNCTION__, __LINE__, t, static_cast<char const*>(m), ##__VA_ARGS__)
109 # define LogWarning(m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_WARNING, static_cast<char const*>(m), ##__VA_ARGS__)
110 # define LogError(m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_ERROR, static_cast<char const*>(m), ##__VA_ARGS__)
111 # define LogFile(m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_FILE, static_cast<char const*>(m), ##__VA_ARGS__)
112 # define LogInfo(m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_INFO, static_cast<char const*>(m), ##__VA_ARGS__)
113 # define LogTrivia(m, ...) Log::Report(0, __FILE__, __FUNCTION__, __LINE__, Log::Type::TYPE_TRIVIA, static_cast<char const*>(m), ##__VA_ARGS__)
114 #endif
Definition: Log.cpp:15
void Report(unsigned int flags, const char *file, const char *function, int line, Type type, const char *str,...)
Definition: Log.cpp:116
Verbosity
Definition: Log.h:52
@ WHISPER
Definition: Log.h:53
@ LOUD
Definition: Log.h:55
@ LOUD_UNSITUATED
Definition: Log.h:54
bool ReportParam(unsigned int test, const char *file, const char *function, int line)
Definition: Log.cpp:200
void Destroy()
Definition: Log.cpp:55
void SetIncludeThreadID(bool inc)
Definition: Log.cpp:109
void SetCustomOutputTargetFunc(void(*textout)(Type, const char *))
Definition: Log.cpp:71
Severity
Definition: Log.h:47
@ BAD
Definition: Log.h:49
@ TERMINAL
Definition: Log.h:50
@ OK
Definition: Log.h:48
void SetVerbosity(Type type, Verbosity verbosity)
Definition: Log.cpp:102
void SetOutputTargets(size_t flags)
Definition: Log.cpp:78
void Init()
Definition: Log.cpp:48
Type
Definition: Log.h:34
@ TYPE_FILE
Definition: Log.h:40
@ TYPE_ASSERT
Definition: Log.h:41
@ N_TYPES
Definition: Log.h:44
@ TYPE_ERROR
Definition: Log.h:39
@ TYPE_PARAM
Definition: Log.h:42
@ TYPE_NEUTRAL
Definition: Log.h:37
@ TYPE_SUCCESS
Definition: Log.h:35
@ TYPE_TRIVIA
Definition: Log.h:43
@ TYPE_WARNING
Definition: Log.h:38
@ TYPE_INFO
Definition: Log.h:36