]> git.stg.codes - stg.git/blob - stglibs/logger.lib/include/stg/logger.h
Explicit prohibition of the PLUGIN_LOGGER assignment.
[stg.git] / stglibs / logger.lib / include / stg / logger.h
1 #ifndef STG_LOGGER_H
2 #define STG_LOGGER_H
3
4 #include <string>
5
6 #include <pthread.h>
7
8 class STG_LOGGER;
9 STG_LOGGER & GetStgLogger();
10 //-----------------------------------------------------------------------------
11 class STG_LOGGER_LOCKER
12 {
13 public:
14     explicit STG_LOGGER_LOCKER(pthread_mutex_t * m) : mutex(m) { pthread_mutex_lock(mutex); }
15     ~STG_LOGGER_LOCKER() { pthread_mutex_unlock(mutex); }
16
17 private:
18     STG_LOGGER_LOCKER(const STG_LOGGER_LOCKER & rvalue);
19     STG_LOGGER_LOCKER & operator=(const STG_LOGGER_LOCKER & rvalue);
20
21     pthread_mutex_t * mutex;
22 };
23 //-----------------------------------------------------------------------------
24 class STG_LOGGER
25 {
26 friend STG_LOGGER & GetStgLogger();
27 friend class PLUGIN_LOGGER;
28
29 public:
30     ~STG_LOGGER();
31     void SetLogFileName(const std::string & fn);
32     void operator()(const char * fmt, ...) const;
33     void operator()(const std::string & line) const { LogString(line.c_str()); }
34
35 private:
36     STG_LOGGER();
37     STG_LOGGER(const STG_LOGGER & rvalue);
38     STG_LOGGER & operator=(const STG_LOGGER & rvalue);
39
40     const char * LogDate(time_t t) const;
41     void LogString(const char * str) const;
42
43     std::string fileName;
44     mutable pthread_mutex_t mutex;
45 };
46 //-----------------------------------------------------------------------------
47 class PLUGIN_LOGGER
48 {
49 friend PLUGIN_LOGGER GetPluginLogger(const STG_LOGGER& logger, const std::string& pluginName);
50
51 public:
52     PLUGIN_LOGGER(const PLUGIN_LOGGER& rhs) : m_parent(rhs.m_parent), m_pluginName(rhs.m_pluginName) {} // Need move here.
53     void operator()(const char* fmt, ...) const;
54     void operator()(const std::string& line) const;
55
56 private:
57     PLUGIN_LOGGER& operator=(const PLUGIN_LOGGER&); // Copy assignment is prohibited.
58
59     PLUGIN_LOGGER(const STG_LOGGER & logger, const std::string & pn);
60     const STG_LOGGER& m_parent;
61     std::string m_pluginName;
62 };
63
64 PLUGIN_LOGGER GetPluginLogger(const STG_LOGGER & logger, const std::string & pluginName);
65
66 #endif //STG_LOGGER_H