Only work with 0.9.4 cocaine
[elliptics.git] / bindings / cpp / log.cpp
blob203421803e556d6b1de4edb37a7c8b4f2a0a996b
1 /*
2 * 2008+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
3 * All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #include <elliptics/cppdef.h>
18 using namespace ioremap::elliptics;
20 void logger::real_logger(void *priv, const int level, const char *msg)
22 logger *log = reinterpret_cast<logger *> (priv);
24 log->log(level, msg);
27 log_file::log_file(const char *file, const int level) :
28 logger(level)
30 try {
31 this->file = new std::string(file);
32 } catch (...) {
33 throw -ENOMEM;
36 try {
37 stream = new std::ofstream(file, std::ios_base::app);
38 } catch (...) {
39 delete this->file;
40 throw -errno;
44 unsigned long log_file::clone(void)
46 return reinterpret_cast<unsigned long>(new log_file (file->c_str(), get_log_level()));
49 log_file::~log_file(void)
51 delete file;
52 delete stream;
55 void log_file::log(int level, const char *msg)
57 if (level < ll.log_level) {
58 char str[64];
59 struct tm tm;
60 struct timeval tv;
61 char usecs_and_id[64];
63 gettimeofday(&tv, NULL);
64 localtime_r((time_t *)&tv.tv_sec, &tm);
65 strftime(str, sizeof(str), "%F %R:%S", &tm);
67 snprintf(usecs_and_id, sizeof(usecs_and_id), ".%06lu %ld/%d : ", tv.tv_usec, dnet_get_id(), getpid());
69 (*stream) << str << usecs_and_id << msg;
70 stream->flush();