The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swlog.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * swlog.cpp -
4  *
5  * $Id: swlog.cpp 3822 2020-11-03 18:54:47Z scribe $
6  *
7  * Copyright 1997-2013 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <iostream>
26 #if defined(_ICU_) && !defined(_ICUSWORD_)
27 #define _USTDIO_
28 #include <unicode/ustdio.h>
29 #include <unicode/ustream.h>
30 #endif
31 #include "swlog.h"
32 #include "swbuf.h"
33 
34 #ifdef USECXX11TIME
35 #include <chrono>
36 using namespace std::chrono;
37 
38 static high_resolution_clock::time_point baseTime = high_resolution_clock::now();
39 #endif
40 
41 
43 
44 
46 
47 const char SWLog::LOG_ERROR = 1;
48 const char SWLog::LOG_WARN = 2;
49 const char SWLog::LOG_INFO = 3;
50 const char SWLog::LOG_TIMEDINFO = 4;
51 const char SWLog::LOG_DEBUG = 5;
52 
54  static class __staticSystemLog {
55  SWLog **clear;
56  public:
57  __staticSystemLog(SWLog **clear) { this->clear = clear; }
58  ~__staticSystemLog() { delete *clear; *clear = 0; }
59  } _staticSystemLog(&SWLog::systemLog);
60 
61  if (!systemLog)
62  systemLog = new SWLog();
63 
64  return systemLog;
65 }
66 
67 
68 void SWLog::setSystemLog(SWLog *newLog) {
69  delete getSystemLog();
70  systemLog = newLog;
71 }
72 
73 
74 void SWLog::logWarning(const char *fmt, ...) const {
75  va_list argptr;
76 
77  if (logLevel >= LOG_WARN) {
78  SWBuf msg;
79  va_start(argptr, fmt);
80  msg.setFormattedVA(fmt, argptr);
81  va_end(argptr);
82  logMessage(msg, LOG_WARN);
83  }
84 }
85 
86 
87 void SWLog::logError(const char *fmt, ...) const {
88  va_list argptr;
89 
90  if (logLevel) {
91  SWBuf msg;
92  va_start(argptr, fmt);
93  msg.setFormattedVA(fmt, argptr);
94  va_end(argptr);
95  logMessage(msg, LOG_ERROR);
96  }
97 }
98 
99 
100 void SWLog::logInformation(const char *fmt, ...) const {
101  va_list argptr;
102 
103  if (logLevel >= LOG_INFO) {
104  SWBuf msg;
105  va_start(argptr, fmt);
106  msg.setFormattedVA(fmt, argptr);
107  va_end(argptr);
108  logMessage(msg, LOG_INFO);
109  }
110 }
111 
112 
113 void SWLog::logTimedInformation(const char *fmt, ...) const {
114  va_list argptr;
115 
116  if (logLevel >= LOG_TIMEDINFO) {
117  const char *fmtStr = fmt;
118  SWBuf msg;
119 #ifdef USECXX11TIME
120  SWBuf msgTS;
121  msgTS.setFormatted("[%.5f] %s", duration_cast<duration<double>>(high_resolution_clock::now() - baseTime).count(), fmt);
122  fmtStr = msgTS.c_str();
123 #endif
124  va_start(argptr, fmt);
125  msg.setFormattedVA(fmtStr, argptr);
126  va_end(argptr);
127  logMessage(msg, LOG_TIMEDINFO);
128  }
129 }
130 
131 
132 void SWLog::logDebug(const char *fmt, ...) const {
133  va_list argptr;
134 
135  if (logLevel >= LOG_DEBUG) {
136  SWBuf msg;
137  va_start(argptr, fmt);
138  msg.setFormattedVA(fmt, argptr);
139  va_end(argptr);
140  logMessage(msg, LOG_DEBUG);
141  }
142 }
143 
144 void SWLog::logMessage(const char *message, int level) const {
145  std::cerr << message;
146  std::cerr << std::endl;
147 }
virtual void logTimedInformation(const char *fmt,...) const
Definition: swlog.cpp:113
#define SWORD_NAMESPACE_START
Definition: defs.h:39
virtual void logMessage(const char *message, int level) const
Definition: swlog.cpp:144
Definition: swbuf.h:47
SWBuf & setFormattedVA(const char *format, va_list argptr)
Definition: swbuf.cpp:61
static SWLog * getSystemLog()
Definition: swlog.cpp:53
static void setSystemLog(SWLog *newLogger)
Definition: swlog.cpp:68
Definition: swlog.h:34
const char * c_str() const
Definition: swbuf.h:158
static SWLog * systemLog
Definition: swlog.h:38
static const char LOG_WARN
Definition: swlog.h:43
void logInformation(const char *fmt,...) const
Definition: swlog.cpp:100
void logWarning(const char *fmt,...) const
Definition: swlog.cpp:74
static long now
Definition: ftpparse.c:48
void logError(const char *fmt,...) const
Definition: swlog.cpp:87
void logDebug(const char *fmt,...) const
Definition: swlog.cpp:132
static const char LOG_INFO
Definition: swlog.h:44
#define SWORD_NAMESPACE_END
Definition: defs.h:40
SWBuf & setFormatted(const char *format,...)
Definition: swbuf.cpp:50
static const char LOG_ERROR
Definition: swlog.h:42
static const char LOG_TIMEDINFO
Definition: swlog.h:45
static const char LOG_DEBUG
Definition: swlog.h:46