torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Log.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 #ifdef HAVE_CONFIG_H
17 #include "torc/generic/config.h"
18 #endif //HAVE_CONFIG_H
19 #ifdef LOGGING
20 #include <cstdio>
21 #include <cstdarg>
22 #endif
23 
24 #include "torc/generic/Log.hpp"
25 
26 namespace {
27 
28 #ifdef LOGGING
29 
30 class Logger {
31 public:
32  void write(const char *fmt, va_list ap);
33 
34  void openLogFile(const std::string& inFileName);
35 
36  Logger();
37  ~Logger();
38 
39  static Logger* instance();
40 
41 private:
42  FILE *mStream;
43 };
44 
45 void Logger::write(const char *fmt, va_list ap) {
46  vfprintf(mStream, fmt, ap);
47  return;
48 }
49 
50 void Logger::openLogFile(const std::string& inFileName) {
51  if(!inFileName.empty()) {
52  FILE *fp = fopen(inFileName.c_str(), "w");
53  if(fp) {
54  if(mStream && mStream != stdout) {
55  fclose(mStream);
56  }
57  mStream = fp;
58  }
59  }
60 }
61 
62 Logger::Logger() : mStream(stdout) {}
63 
64 Logger::~Logger() {
65  if(mStream && mStream != stdout) {
66  fclose(mStream);
67  mStream = NULL;
68  }
69 }
70 
71 Logger* Logger::instance() {
72  static Logger obj;
73  return &obj;
74 }
75 
76 #endif //LOGGING
77 }
78 
79 namespace torc {
80 namespace generic {
81 
82 void openLogFile(const std::string& logFileName) {
83 #ifdef LOGGING
84  Logger::instance()->openLogFile(logFileName);
85 #endif
86  return;
87 }
88 
89 void log(const char *fmt, ...) {
90 #ifdef LOGGING
91  va_list args;
92  va_start(args, fmt);
93  Logger::instance()->write(fmt, args);
94  va_end(args);
95 #endif
96  return;
97 }
98 
99 } //namespace torc
100 } //namespace generic
101 
void log(const char *fmt,...)
Definition: Log.cpp:89
std::string string
void openLogFile(const std::string &logFileName)
Definition: Log.cpp:82