log.cxx

00001 #include <iostream>
00002 // #include <fstream>
00003 #include <stdexcept>
00004 
00005 #include <unistd.h>
00006 #include <stdio.h> // For tempnam
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 Log::Log(std::string pref, int instance) {
00028   {
00029     char *tempName = tempnam(NULL, pref.c_str());
00030     std::cout << "Log started to file named something like \"" << tempName << "\"\n";
00031     open(tempName, std::ios::app);
00032     free(tempName);
00033   }
00034   printHeader();
00035   *this << "  logging to: \"" << pref << "\"" << std::endl;
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"; // ucid
00055     return fileName;
00056 }
00057 
00058 void Log::printHeader() {
00059   *this << "  pid: " << (int)getpid() << "\n"; // << flush;
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 }

Generated on Mon Feb 6 14:01:22 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6