Loading...
Searching...
No Matches
gamslog.h
1
26#ifndef GAMSLOG_H
27#define GAMSLOG_H
28
29#include <map>
30#include <sstream>
31#include <unordered_set>
32#include "gamsenum.h"
33#include "gamslib_global.h"
34
35namespace gams {
36
39{
40 struct TargetSet {
41 TargetSet() : mDebug(GAMSEnum::DebugLevel::Verbose) {}
42 TargetSet(const GAMSEnum::DebugLevel debug, FILE *target = 0) : mDebug(debug) {
43 mTargets.insert(mTargets.end(), (target ? target : stdout));
44 }
46 std::unordered_set<FILE*> mTargets;
47 };
48
49public:
50
54
59 void registerLogger(const LogId logId, const GAMSEnum::DebugLevel debug, FILE *target = stdout);
60
63 void unregisterLogger(const LogId logId);
64
69 std::unordered_set<FILE*> targets(const LogId logId, const GAMSEnum::DebugLevel debug) const {
70 if (mBinds.find(logId) == mBinds.end())
71 return std::unordered_set<FILE*>();
72 TargetSet lc = mBinds.at(logId);
73 if (lc.mDebug < debug)
74 return std::unordered_set<FILE*>();
75 return lc.mTargets;
76 }
77
81 GAMSEnum::DebugLevel debug(const LogId logId) const {
82 if (mBinds.count(logId) > 0)
83 return mBinds.at(logId).mDebug;
84 else
86 }
87
88private:
89 static LoggerPool *mInstance;
90 std::map<LogId, TargetSet> mBinds;
91 LoggerPool() {}
92 LoggerPool(LoggerPool const&) {}
93 void operator=(LoggerPool const&) {}
94};
95
97class Logger
98{
99public:
100
105 Logger(const LogId logID, const GAMSEnum::DebugLevel debug, const char* where)
106 : mBufferStream(mBuffer), mWhere(where), mTargets(LoggerPool::instance().targets(logID, debug))
107 {}
108
111
114 Logger& operator <<(std::ostream& (*os)(std::ostream&)) {
115 std::stringstream strStream;
116 strStream << os;
117 mBufferStream << strStream.str().c_str();
118 return *this;
119 }
120
123 Logger& operator <<(const std::string& value) {
124 mBufferStream << value.c_str();
125 return *this;
126 }
127
130 template<typename T>
131 Logger& operator <<(const T& value) {
132 mBufferStream << value;
133 return *this;
134 }
135
136private:
137 std::string mBuffer;
138 std::stringstream mBufferStream;
139 std::string mWhere;
140 std::unordered_set<FILE*> mTargets;
141};
142
143}
144
145#define DEB_S(logID) ::gams::Logger(logID, ::gams::GAMSEnum::DebugLevel::Verbose, __FUNCTION__)
146#define DEB ::gams::Logger(logID(), ::gams::GAMSEnum::DebugLevel::Verbose, __FUNCTION__)
147#define MSG ::gams::Logger(logID(), ::gams::GAMSEnum::DebugLevel::ShowLog, __FUNCTION__)
148#define ERR ::gams::Logger(logID(), ::gams::GAMSEnum::DebugLevel::Off, __FUNCTION__)
149//#define MSG(ws) ::gams::Logger(ws, ::gams::GAMSEnum::DebugLevel::ShowLog, __FUNCTION__)
150
151#endif // GAMSLOG_H
DebugLevel
GAMS Debug Level.
Definition gamsenum.h:198
@ Verbose
Send highly technical info and GAMS log to stdout and keep temporary file.
Definition gamsenum.h:203
@ Off
No Debug.
Definition gamsenum.h:199
The class to manage all loggers used in an API instance.
Definition gamslog.h:39
static LoggerPool & instance()
GAMSEnum::DebugLevel debug(const LogId logId) const
Definition gamslog.h:81
void registerLogger(const LogId logId, const GAMSEnum::DebugLevel debug, FILE *target=stdout)
void unregisterLogger(const LogId logId)
std::unordered_set< FILE * > targets(const LogId logId, const GAMSEnum::DebugLevel debug) const
Definition gamslog.h:69
Logging class used to get feedback about the API actions.
Definition gamslog.h:98
Logger(const LogId logID, const GAMSEnum::DebugLevel debug, const char *where)
Definition gamslog.h:105
~Logger()
Destructor.
Logger & operator<<(std::ostream &(*os)(std::ostream &))
Definition gamslog.h:114
Definition gams.h:91