File: log.cpp

package info (click to toggle)
cyphesis-cpp 0.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 10,752 kB
  • sloc: cpp: 94,194; xml: 40,196; python: 8,717; sh: 4,164; makefile: 1,968; ansic: 753
file content (262 lines) | stat: -rw-r--r-- 6,070 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Cyphesis Online RPG Server and AI Engine
// Copyright (C) 2000-2003 Alistair Riddoch
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "log.h"
#include "globals.h"
#include "compose.hpp"

#include <iostream>
#include <fstream>

#include <cstring>
#include <ctime>

extern "C" {
#ifdef HAVE_SYSLOG_H
  #include <syslog.h>
#endif // HAVE_SYSLOG_H
  #include <errno.h>
}

#ifndef _WIN32
static const char * TIME_FORMAT = "%FT%T";
#else
static const char * TIME_FORMAT = "%Y-%m-%dT%H:%M:%S";
#endif

static void logDate(std::ostream & log_stream)
{
    struct tm * local_time;
    const time_t now = time(NULL);

#ifdef HAVE_LOCALTIME_R

    struct tm local_time_buffer;

    local_time = &local_time_buffer;

    if (localtime_r(&now, local_time) != local_time) {
        log_stream << "[TIME_ERROR]";
        return;
    }
#else // HAVE_LOCALTIME_R
    local_time = localtime(&now);
#endif // HAVE_LOCALTIME_R

    char buf[256];
    size_t count = strftime(buf, sizeof(buf) / sizeof(char), TIME_FORMAT, local_time);

    if (count == 0) {
        log_stream << "[TIME_ERROR]";
        return;
    }

    log_stream << buf;
}

static std::ofstream event_log;

static void open_event_log()
{
    std::string event_log_file = var_directory + "/log/cyphesis/cyphesis_event.log";

    event_log.open(event_log_file.c_str(), std::ios::out | std::ios::app);

    if (!event_log.is_open()) {
        log(ERROR, String::compose("Unable to open event log file \"%1\"",
                                   event_log_file));
        logSysError(ERROR);
    }
}

bool testEventLog(const char * path)
{
   event_log.open(path, std::ios::out);
   return event_log.is_open();
}

void initLogger()
{
#ifdef HAVE_SYSLOG
    std::string ident = String::compose("Cyphesis{%1}", instance);
    if (daemon_flag) {
        openlog(strdup(ident.c_str()), LOG_PID, LOG_DAEMON);
    }
#endif // HAVE_SYSLOG

    open_event_log();
}

void rotateLogger()
{
    if (event_log.is_open()) {
        event_log.close();
    }

    open_event_log();
}

std::ostream & operator<<(std::ostream & s, LogLevel lvl)
{
    switch (lvl) {
        case INFO:
            s << "INFO";
            break;
        case SCRIPT:
            s << "SCRIPT";
            break;
        case NOTICE:
            s << "NOTICE";
            break;
        case WARNING:
            s << "WARNING";
            break;
        case ERROR:
            s << "ERROR";
            break;
        case SCRIPT_ERROR:
            s << "SCRIPT_ERROR";
            break;
        case CRITICAL:
            s << "CRITICAL";
            break;
        default:
            s << "UNKNOWN";
            break;
    };
    return s;
}

void log(LogLevel lvl, const std::string & msg)
{
#ifdef HAVE_SYSLOG
    if (daemon_flag) {
        int type;
        switch (lvl) {
            case SCRIPT:
            case INFO:
                type = LOG_INFO;
                break;
            case NOTICE:
                type = LOG_NOTICE;
                break;
            case WARNING:
                type = LOG_WARNING;
                break;
            case SCRIPT_ERROR:
            case ERROR:
                type = LOG_ERR;
                break;
            case CRITICAL:
                type = LOG_CRIT;
                break;
            default:
                type = LOG_CRIT;
                break;
        };
        syslog(type, "%s", msg.c_str());
    } else {
#else // HAVE_SYSLOG
    {
#endif // HAVE_SYSLOG

        logDate(std::cerr);
        std::cerr << " " << lvl << " " << msg << std::endl << std::flush;
    }
}

void log_formatted(LogLevel lvl, const std::string & msg)
{
    std::string::size_type s = 0;
    std::string::size_type p = msg.find('\n');
    do {
        log(lvl, msg.substr(s, p - s));
        s = p + 1;
        p = msg.find('\n', s);
    } while (p != std::string::npos);

    // If the last line is not terminated with a newline, print it.
    if (s < msg.size()) {
        log(lvl, msg.substr(s));
    }
}

void logEvent(LogEvent lev, const std::string & msg)
{
    if (!event_log.is_open()) {
        return;
    }

    const char * type;
    switch (lev) {
        case START:
            type = "START";
            break;
        case STOP:
            type = "STOP";
            break;
        case CONNECT:
            type = "CONNECT";
            break;
        case DISCONNECT:
            type = "DISCONNECT";
            break;
        case LOGIN:
            type = "LOGIN";
            break;
        case LOGOUT:
            type = "LOGOUT";
            break;
        case TAKE_CHAR:
            type = "TAKE_CHAR";
            break;
        case DROP_CHAR:
            type = "DROP_CHAR";
            break;
        case EXPORT_ENT:
            type = "EXPORT_ENT";
            break;
        case IMPORT_ENT:
            type = "IMPORT_ENT";
            break;
        case POSSESS_CHAR:
            type = "POSSESS_CHAR";
            break;
        default:
            type = "UNKNOWN";
            break;
    };

    logDate(event_log);
    event_log << " " << instance << " " << type << " " << msg
              << std::endl << std::flush;
}

void logSysError(LogLevel lvl)
{
    char * err = strerror(errno);
    if (err != NULL) {
        log(lvl, err);
    } else {
        log(ERROR, "Error getting error message from system.");
    }
}