Files
geant4/source/externals/g4tools/include/tools/log_file
T
2021-06-25 16:12:29 +02:00

61 lines
1.3 KiB
Plaintext

// Copyright (C) 2010, Guy Barrand. All rights reserved.
// See the file tools.license for terms.
#ifndef tools_log_file
#define tools_log_file
#include <cstdio>
#include "sys/process"
#include "sprintf"
namespace tools {
class log_file {
public:
static log_file& get() {
static log_file s_log_file;
return s_log_file;
}
private:
log_file():m_FILE(0) {
#ifdef _WIN32
#if defined(_MSC_VER) && _MSC_VER < 1900
std::string file_name("C:\\cygwin\\tmp\\inlib_log_file_");
#else
std::string file_name("C:\\cygwin64\\tmp\\inlib_log_file_");
#endif
#else
std::string file_name("/tmp/inlib_log_file_");
#endif
std::string spid;
sprintf(spid,128,"%d",process_id());
file_name += spid;
m_FILE = ::fopen(file_name.c_str(),"wb");
}
~log_file() {if(m_FILE) ::fclose(m_FILE);}
protected:
log_file(const log_file&):m_FILE(0) {}
log_file& operator=(const log_file&) {return *this;}
public:
void close() {if(m_FILE) ::fclose(m_FILE);m_FILE = 0;}
void write(const std::string& a_string) const {
if(!m_FILE) return;
if(::fprintf(m_FILE,"%s",a_string.c_str())<0) {}
}
void writef(const char* a_format,...) const {
if(!m_FILE) return;
std::string s;
va_list args;
va_start(args,a_format);
if(vsprintf(s,1000,a_format,args)) write(s);
va_end(args);
}
protected:
FILE* m_FILE;
};
}
#endif