libs/EDoc/Logger.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 
00003    Copyright (C) 2007 by Brendon Costa
00004 
00005    This library is free software; you can redistribute it and/or modify 
00006    it under the terms of the "LGPL Like" License defined in the file COPYING 
00007    that should have been distributed along with this source.
00008 
00009    This library is distributed in the hope that it will be useful, 
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of 
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
00012 
00013    You should have received a copy of the "LGPL Like" License 
00014    along with this library; see the file COPYING. if not, it can be 
00015    obtained from the EDoc++ website: 
00016    http://edoc.sourceforge.net/license.html
00017 
00018 *******************************************************************************/
00019 #include "config.h"
00020 
00021 #include "EDoc/Logger.h"
00022 #include "EDoc/exceptions.h"
00023 #include "EDoc/utils.h"
00024 
00025 //getenv
00026 #ifdef HAVE_STDLIB_H
00027    #include <stdlib.h>
00028 #endif
00029 
00030 //strcasecmp
00031 #ifdef HAVE_STRING_H
00032    #include <string.h>
00033 #endif
00034 
00035 #ifdef HAVE_STRINGS_H
00036    #include <strings.h>
00037 #endif
00038 
00039 namespace EDoc
00040 {
00041    //========================================================================
00042    Logger* Logger::instance = NULL;
00043    //========================================================================
00044 
00045 
00046    //========================================================================
00047    Logger::Logger() : 
00048       use_stdout(false),
00049       use_stderr(false),
00050       use_file(true),
00051       file_initialised(false)
00052    {
00053       const char* value = NULL;
00054       value = getenv("EDOC_LOGGER_STDERR");
00055       if (value)
00056       {
00057          use_stderr = !strcasecmp(value, "yes");
00058       }
00059 
00060       value = getenv("EDOC_LOGGER_STDOUT");
00061       if (value)
00062       {
00063          use_stdout = !strcasecmp(value, "yes");
00064       }
00065 
00066       filename="";
00067       use_file = true;
00068       value = getenv("EDOC_LOGGER_FILE");
00069       if (value)
00070       {
00071          use_file = strcasecmp(value, "no");
00072          if (use_file)
00073          {
00074             filename = value;
00075          }
00076       }
00077       
00078       if (use_file)
00079       {
00080          if (filename == "")
00081          {
00082             filename = MakeTemporaryFile("edoc_", ".log");
00083          }
00084          use_file = SetLogFilenameFullyQualified(filename);
00085       }
00086    }
00087    //========================================================================
00088    Logger::~Logger()
00089    {
00090       if (use_file)
00091       {
00092          file.close();
00093       }
00094    }
00095    //========================================================================
00096    bool Logger::SetLogFilenameFullyQualified(std::string filename_in)
00097    {
00098       // Close the old file if open.
00099       if (file)
00100       {
00101          file.close();
00102       }
00103 
00104       use_file = true;
00105       filename = filename_in;
00106       file_initialised = false;
00107       return true;
00108    }
00109    //===========================================================================
00110    void Logger::InitialiseFile()
00111    {
00112       // Close the old file if there was one.
00113       file.close();
00114       file.clear();
00115       
00116       // Open the new file.
00117       file.open(filename.c_str(), std::ios::out);
00118       if (!file)
00119       {
00120          EDOC_THROW_EXCEPTION(BugException, 
00121             "Failed to initialise logging framework.", "");
00122       }
00123 
00124       std::cerr << "Logs going to file: " << filename << std::endl;
00125       file_initialised = true;
00126    }
00127    //===========================================================================
00128 }

Generated on Tue Jan 20 18:26:07 2009 for EDoc-0.2.1 by  doxygen 1.5.1