00001 #include <iostream>
00002
00003 #include <stdexcept>
00004
00005 #include <unistd.h>
00006 #include <stdio.h>
00007
00008 #include "log.h"
00009 #include "Sct/StringStreamer.h"
00010
00011 namespace SctApi {
00012 Log::Log(const Sct::UCID & ucid) :
00013 m_ucid(ucid) {
00014 std::string fileName = getDefaultFileName();
00015 open(fileName.c_str(), std::ios::app);
00016 std::cout << "Log started to " << fileName << std::endl;
00017 printHeader();
00018 }
00019
00020 Log::Log(const Sct::UCID & ucid, std::string fname) : std::ofstream(fname.c_str(), std::ios::app), m_ucid(ucid) {
00021 std::cout << "Log started to \"" << fname.c_str() << "\"\n";
00022 printHeader();
00023 *this << " logging to: \"" << fname << "\"" << std::endl;
00024 }
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 Log::~Log() {
00040 if(std::uncaught_exception()) {
00041 *this << "Logging shut down due to exception\n";
00042 } else {
00043 *this << "Logging successfully shut down\n";
00044 }
00045 printTrailer();
00046
00047 close();
00048 }
00049
00050 std::string Log::getDefaultFileName() const {
00051 char* logdir = getenv("TDAQ_LOGS_PATH");
00052 if (!logdir)
00053 logdir = "/tmp";
00054 const std::string fileName = std::string(logdir) + "/SctApiLog." + static_cast<std::string>(Sct::StringStreamer<<m_ucid) + ".log";
00055 return fileName;
00056 }
00057
00058 void Log::printHeader() {
00059 *this << " pid: " << (int)getpid() << "\n";
00060 time_t currTime = time(0);
00061 *this << " at: " << ctime(&currTime) << std::endl;
00062 }
00063
00064 void Log::printTrailer() {
00065 *this << " pid = " << ((int)getpid()) << std::endl;
00066 time_t currTime = time(0);
00067 *this << " at: " << ctime(&currTime) << std::endl;
00068 }
00069 }