forked from Softmotions/ejdb-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathejdb_logging.cc
More file actions
160 lines (141 loc) · 5.06 KB
/
ejdb_logging.cc
File metadata and controls
160 lines (141 loc) · 5.06 KB
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
/**************************************************************************************************
* EJDB database library http://ejdb.org
* Copyright (C) 2012-2013 Softmotions Ltd <info@softmotions.com>
*
* This file is part of EJDB.
* EJDB is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License or any later version. EJDB 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 Lesser General Public
* License for more details.
* You should have received a copy of the GNU Lesser General Public License along with EJDB;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA.
*************************************************************************************************/
#include "ejdb_logging.h"
#include "ejdb_thread.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#ifdef _WIN32
#include <io.h>
static void __flockfile(FILE *stream) {
HANDLE hf;
int fd;
LARGE_INTEGER li;
fd = _fileno(stream);
hf = (HANDLE) _get_osfhandle(fd);
li.QuadPart = _filelengthi64(fd);
LockFile(hf, 0, 0, li.LowPart, li.HighPart);
}
static void __funlockfile(FILE *stream) {
HANDLE hf;
int fd;
LARGE_INTEGER li;
fd = _fileno(stream);
hf = (HANDLE) _get_osfhandle(fd);
li.QuadPart = _filelengthi64(fd);
UnlockFile(hf, 0, 0, li.LowPart, li.HighPart);
}
#endif
#if defined __unix || defined __APPLE__
static void __flockfile(FILE *stream) {
flockfile(stream);
}
static void __funlockfile(FILE *stream) {
funlockfile(stream);
}
#endif
namespace ejdb {
EJMutex g_outputMtx;
const char* ej_errno_msg(int errno_rv) {
return strerror(errno_rv);
}
void ej_log_intern(int priority, const char* file, int line, const char* fmt, ...) {
#ifndef _DEBUG
if (priority == LOG_DEBUG) {
return;
}
#endif
//Timestamp
char tbuff[64];
time_t rawtime;
struct tm* timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(tbuff, 64, "%d %b %H:%M:%S -", timeinfo);
__flockfile(stderr);
__flockfile(stdout);
try {
switch (priority) {
case LOG_ERROR:
if (file && line > 0) {
fprintf(stderr, "%s ERROR %s:%d ", tbuff, file, line);
} else {
fprintf(stderr, "%s ERROR ", tbuff);
}
break;
case LOG_DEBUG:
if (file && line > 0) {
fprintf(stderr, "%s DEBUG %s:%d ", tbuff, file, line);
} else {
fprintf(stderr, "%s DEBUG ", tbuff);
}
break;
case LOG_INFO:
if (file && line > 0) {
fprintf(stderr, "%s INFO %s:%d ", tbuff, file, line);
} else {
fprintf(stderr, "%s INFO ", tbuff);
}
break;
case LOG_WARNING:
if (file && line > 0) {
fprintf(stderr, "%s WARN %s:%d ", tbuff, file, line);
} else {
fprintf(stderr, "%s WARN ", tbuff);
}
break;
default:
if (file && line > 0) {
fprintf(stderr, "%s %s:%d ", tbuff, file, line);
} else {
fprintf(stderr, "%s ", tbuff);
}
}
va_list vl;
va_start(vl, fmt);
vfprintf(stderr, fmt, vl);
va_end(vl);
fprintf(stderr, "\n");
} catch (...) {
__funlockfile(stderr);
__funlockfile(stdout);
throw;
}
__funlockfile(stderr);
__funlockfile(stdout);
}
void ej_log_errno_status(const char* file, int line, int errno_rv, const std::string& msg) {
ej_log_errno_status(file, line, errno_rv, msg.c_str());
}
void ej_log_errno_status(const char* file, int line, int errno_rv, const char* msg) {
if (msg) {
ej_log_intern(LOG_WARNING, file, line, "%s, %s", msg, strerror(errno_rv));
} else {
ej_log_intern(LOG_WARNING, file, line, "%s", strerror(errno_rv));
}
}
void ej_log_ejerror(const char* file, int line, const EJError& err) {
ej_log_errno_status(file, line, err.errno_code, err.msg);
}
void EJError::errPrint() {
ej_log_intern(LOG_ERROR, this->location.c_str(), (int) this->line, "%s, %s",
this->msg.c_str(),
strerror(this->errno_code));
}
} //eof ejdb namespace