0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Logger.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
29 #include <Common/Compat.h>
30 
31 #include "String.h"
32 #include "Logger.h"
33 
34 #include <iostream>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <mutex>
38 
39 namespace Hypertable { namespace Logger {
40 
42 static LogWriter *logger_obj = 0;
44 
45 void initialize(const String &name) {
46  logger_name = name;
47 }
48 
49 LogWriter *get() {
50  if (!logger_obj)
51  logger_obj = new LogWriter(logger_name);
52  return logger_obj;
53 }
54 
55 void LogWriter::log_string(int priority, const char *message) {
56  static const char *priority_name[] = {
57  "FATAL",
58  "ALERT",
59  "CRIT",
60  "ERROR",
61  "WARN",
62  "NOTICE",
63  "INFO",
64  "DEBUG",
65  "NOTSET"
66  };
67 
68  std::lock_guard<std::mutex> lock(mutex);
69  if (m_test_mode) {
70  fprintf(m_file, "%s %s : %s\n", priority_name[priority], m_name.c_str(),
71  message);
72  }
73  else {
74  time_t t = ::time(0);
75  fprintf(m_file, "%u %s %s : %s\n", (unsigned)t, priority_name[priority],
76  m_name.c_str(), message);
77  }
78 
79  flush();
80 }
81 
82 void LogWriter::log_varargs(int priority, const char *format, va_list ap) {
83  char buffer[1024 * 16];
84  vsnprintf(buffer, sizeof(buffer), format, ap);
85  log_string(priority, buffer);
86 }
87 
88 void LogWriter::debug(const char *format, ...) {
89  va_list ap;
90  va_start(ap, format);
91  log_varargs(Priority::DEBUG, format, ap);
92  va_end(ap);
93 }
94 
95 void LogWriter::log(int priority, const char *format, ...) {
96  va_list ap;
97  va_start(ap, format);
98  log_varargs(priority, format, ap);
99  va_end(ap);
100 }
101 
102 }} // namespace Hypertable::
void log_string(int priority, const char *message)
Appends a string message to the log.
Definition: Logger.cc:55
static std::mutex mutex
Definition: Logger.cc:43
void initialize(const String &name)
Public initialization function - creates a singleton instance of LogWriter.
Definition: Logger.cc:45
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
String format(const char *fmt,...)
Returns a String using printf like format facilities Vanilla snprintf is about 1.5x faster than this...
Definition: String.cc:37
void debug(const char *format,...)
Prints a debug message with variable arguments (similar to printf)
Definition: Logger.cc:88
void log_varargs(int priority, const char *format, va_list ap)
Appends a string message with variable arguments to the log.
Definition: Logger.cc:82
String m_name
The name of the application.
Definition: Logger.h:147
Logging routines and macros.
Compatibility Macros for C/C++.
void flush()
Flushes the log file.
Definition: Logger.h:113
static String logger_name
Definition: Logger.cc:41
FILE * m_file
The output file handle.
Definition: Logger.h:153
The LogWriter class writes to stdout.
Definition: Logger.h:70
Hypertable definitions
void log(int priority, const char *format,...)
Prints a message with variable arguments.
Definition: Logger.cc:95
A String class based on std::string.
static LogWriter * logger_obj
Definition: Logger.cc:42
bool m_test_mode
True if this log is in test mode.
Definition: Logger.h:144