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