gamslog.h
1/*
2  * GAMS - General Algebraic Modeling System C++ API
3  *
4  * Copyright (c) 2017-2022 GAMS Software GmbH <support@gams.com>
5  * Copyright (c) 2017-2022 GAMS Development Corp. <support@gams.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #ifndef GAMSLOG_H
27 #define GAMSLOG_H
28 
29 #include <sstream>
30 #include <unordered_set>
31 #include "gamsenum.h"
32 #include "gamslib_global.h"
33 
34 namespace gams {
35 
38 {
39  struct TargetSet {
40  TargetSet() : mDebug(GAMSEnum::DebugLevel::Verbose) {}
41  TargetSet(const GAMSEnum::DebugLevel debug, FILE *target = 0) : mDebug(debug) {
42  mTargets.insert(mTargets.end(), (target ? target : stdout));
43  }
44  GAMSEnum::DebugLevel mDebug;
45  std::unordered_set<FILE*> mTargets;
46  };
47 
48 public:
49 
52  static LoggerPool &instance();
53 
58  void registerLogger(const LogId logId, const GAMSEnum::DebugLevel debug, FILE *target = stdout);
59 
62  void unregisterLogger(const LogId logId);
63 
68  std::unordered_set<FILE*> targets(const LogId logId, const GAMSEnum::DebugLevel debug) const {
69  if (mBinds.find(logId) == mBinds.end())
70  return std::unordered_set<FILE*>();
71  TargetSet lc = mBinds.at(logId);
72  if (lc.mDebug < debug)
73  return std::unordered_set<FILE*>();
74  return lc.mTargets;
75  }
76 
80  GAMSEnum::DebugLevel debug(const LogId logId) const {
81  if (mBinds.count(logId) > 0)
82  return mBinds.at(logId).mDebug;
83  else
84  return GAMSEnum::DebugLevel::Off;
85  }
86 
87 private:
88  static LoggerPool *mInstance;
89  std::map<LogId, TargetSet> mBinds;
90  LoggerPool() {}
91  LoggerPool(LoggerPool const&) {}
92  void operator=(LoggerPool const&) {}
93 };
94 
96 class Logger
97 {
98 public:
99 
104  Logger(const LogId logID, const GAMSEnum::DebugLevel debug, const char* where)
105  : mBufferStream(mBuffer), mWhere(where), mTargets(LoggerPool::instance().targets(logID, debug))
106  {}
107 
110 
113  Logger& operator <<(std::ostream& (*os)(std::ostream&)) {
114  std::stringstream strStream;
115  strStream << os;
116  mBufferStream << strStream.str().c_str();
117  return *this;
118  }
119 
122  Logger& operator <<(const std::string& value) {
123  mBufferStream << value.c_str();
124  return *this;
125  }
126 
129  template<typename T>
130  Logger& operator <<(const T& value) {
131  mBufferStream << value;
132  return *this;
133  }
134 
135 private:
136  std::string mBuffer;
137  std::stringstream mBufferStream;
138  std::string mWhere;
139  std::unordered_set<FILE*> mTargets;
140 };
141 
142 }
143 
144 #define DEB_S(logID) ::gams::Logger(logID, ::gams::GAMSEnum::DebugLevel::Verbose, __FUNCTION__)
145 #define DEB ::gams::Logger(logID(), ::gams::GAMSEnum::DebugLevel::Verbose, __FUNCTION__)
146 #define MSG ::gams::Logger(logID(), ::gams::GAMSEnum::DebugLevel::ShowLog, __FUNCTION__)
147 #define ERR ::gams::Logger(logID(), ::gams::GAMSEnum::DebugLevel::Off, __FUNCTION__)
148 //#define MSG(ws) ::gams::Logger(ws, ::gams::GAMSEnum::DebugLevel::ShowLog, __FUNCTION__)
149 
150 #endif // GAMSLOG_H
void registerLogger(const LogId logId, const GAMSEnum::DebugLevel debug, FILE *target=stdout)
static LoggerPool & instance()
Definition: gams.h:91
void unregisterLogger(const LogId logId)
GAMSEnum::DebugLevel debug(const LogId logId) const
Definition: gamslog.h:80
Logger & operator<<(std::ostream &(*os)(std::ostream &))
Definition: gamslog.h:113
std::unordered_set< FILE * > targets(const LogId logId, const GAMSEnum::DebugLevel debug) const
Definition: gamslog.h:68
Logger(const LogId logID, const GAMSEnum::DebugLevel debug, const char *where)
Definition: gamslog.h:104
Logging class used to get feedback about the API actions.
Definition: gamslog.h:97
The class to manage all loggers used in an API instance.
Definition: gamslog.h:38
DebugLevel
GAMS Debug Level.
Definition: gamsenum.h:198
~Logger()
Destructor.