Merge commit 'origin/master'
[versaplex.git] / vxodbc / wvlogger.cc
blob1581bbc295cc2de0dbef415f46258e75d51134f8
1 #include "wvlogger.h"
2 #include <wvlog.h>
3 #include <wvlogfile.h>
4 #include <wvlogstream.h>
5 #include <wvmoniker.h>
6 #include <wvlinkerhack.h>
7 #include <wvistreamlist.h>
8 #include <wvcrash.h>
9 #include "psqlodbc.h"
10 #include <wvlogrcv.h>
12 static WvLog *wvlog = NULL;
13 static WvLogRcv *rcv = NULL;
15 int log_level = 0;
16 static WvString log_moniker;
18 WV_LINK_TO(WvConStream);
19 WV_LINK_TO(WvTCPConn);
20 WV_LINK_TO(WvSSLStream);
21 WV_LINK_TO(WvGzipStream);
22 #ifndef _MSC_VER
23 WV_LINK_TO(WvUnixConn);
24 #endif
26 struct pstring wvlog_get_moniker()
28 const int safe_size = 1024;
29 log_moniker.setsize(safe_size);
30 struct pstring ret = { log_moniker.edit(), safe_size };
31 return ret;
34 int wvlog_isset()
36 return !!log_moniker;
39 class WvNullRcv : public WvLogRcv
41 public:
42 WvNullRcv() : WvLogRcv(WvLog::Info) {}
43 ~WvNullRcv() {}
45 protected:
46 virtual void _mid_line(const char *str, size_t len) {}
49 void wvlog_open()
51 if (rcv)
52 wvlog_close();
53 #ifdef _MSC_VER
54 else // This will only be true once; when we first call this function
55 setup_console_crash();
56 #endif
58 if (wvlog_isset())
60 WvLog::LogLevel pri;
61 if (log_level >= (int)WvLog::NUM_LOGLEVELS)
62 pri = WvLog::Debug5;
63 else if (log_level >= 1)
64 pri = (WvLog::LogLevel)log_level;
65 else
66 pri = WvLog::Info;
68 IWvStream *s = wvcreate<IWvStream>(log_moniker);
69 assert(s);
70 rcv = new WvLogStream(s, pri);
71 if (!wvlog)
72 wvlog = new WvLog(getpid(), WvLog::Debug);
74 (*wvlog)(WvLog::Notice, "Log initialized. (log_level=%s)\n",
75 log_level);
77 else // We want this to also capture (and eliminate) DBus messages.
78 rcv = new WvNullRcv();
82 void wvlog_print(const char *file, int line, const char *s)
84 if (!wvlog)
85 return;
86 while (WvIStreamList::globallist.select(0))
87 WvIStreamList::globallist.callback();
88 WvString ss("%s:%s: %s", file, line, s);
89 wvlog->print(ss);
93 void wvlog_close()
95 if (wvlog) (*wvlog)(WvLog::Info, "Log closing.\n");
96 if (wvlog) delete wvlog;
97 if (rcv) delete rcv;
98 wvlog = NULL;
99 rcv = NULL;