g4tools  5.4.0
log_file
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_log_file
5 #define tools_log_file
6 
7 #include <cstdio>
8 
9 #include "sys/process"
10 #include "sprintf"
11 
12 namespace tools {
13 
14 class log_file {
15 public:
16  static log_file& get() {
17  static log_file s_log_file;
18  return s_log_file;
19  }
20 private:
21  log_file():m_FILE(0) {
22 #ifdef _WIN32
23 #if defined(_MSC_VER) && _MSC_VER < 1900
24  std::string file_name("C:\\cygwin\\tmp\\inlib_log_file_");
25 #else
26  std::string file_name("C:\\cygwin64\\tmp\\inlib_log_file_");
27 #endif
28 #else
29  std::string file_name("/tmp/inlib_log_file_");
30 #endif
31  std::string spid;
32  sprintf(spid,128,"%d",process_id());
33  file_name += spid;
34  m_FILE = ::fopen(file_name.c_str(),"wb");
35  }
36  ~log_file() {if(m_FILE) ::fclose(m_FILE);}
37 protected:
38  log_file(const log_file&):m_FILE(0) {}
39  log_file& operator=(const log_file&) {return *this;}
40 public:
41  void close() {if(m_FILE) ::fclose(m_FILE);m_FILE = 0;}
42  void write(const std::string& a_string) const {
43  if(!m_FILE) return;
44  if(::fprintf(m_FILE,"%s",a_string.c_str())<0) {}
45  }
46  void writef(const char* a_format,...) const {
47  if(!m_FILE) return;
48  std::string s;
49  va_list args;
50  va_start(args,a_format);
51  if(vsprintf(s,1000,a_format,args)) write(s);
52  va_end(args);
53  }
54 protected:
55  FILE* m_FILE;
56 };
57 
58 }
59 
60 #endif
tools::log_file::writef
void writef(const char *a_format,...) const
Definition: log_file:46
tools::vsprintf
bool vsprintf(std::string &a_string, int a_length, const char *a_format, va_list a_args)
Definition: sprintf:12
tools::log_file::close
void close()
Definition: log_file:41
tools::log_file
Definition: log_file:14
tools::log_file::m_FILE
FILE * m_FILE
Definition: log_file:55
tools::process_id
int process_id()
Definition: process:15
process
tools::args
Definition: args:24
tools::file_name
bool file_name(const std::string &a_path, std::string &a_name)
Definition: file_name:61
sprintf
tools::log_file::operator=
log_file & operator=(const log_file &)
Definition: log_file:39
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::log_file::get
static log_file & get()
Definition: log_file:16
tools::sprintf
bool sprintf(std::string &a_string, int a_length, const char *a_format,...)
Definition: sprintf:34
tools::log_file::write
void write(const std::string &a_string) const
Definition: log_file:42
tools::log_file::log_file
log_file(const log_file &)
Definition: log_file:38