| Unuk 1.0

- Main Page
- Classes
- Files

- File List
- File Members

src/libUnuk/Debug.cpp
Go to the documentation of this file.

    00001 #include <iostream>
    00002 #include <fstream>
    00003 #include <cstdarg>
    00004 #include <ctime>
    00005 #include "Debug.h"
    00006 #include "string"
    00007
    00008
    00009 using namespace std;
    00010
    00011 // ===================================================================
    00012 // The Debug log allows us to display ever piece of data that
    00013 // populates our class components, anything that is loaded, serialized,
    00014 // de-serialized etc will be printed out to a text file.
    00015 // (Running our program in a terminal, this debug log will print to it.)
    00016 // ===================================================================
    00017
    00018 Debug *Debug::logger = NULL;
    00019
    00020 Debug::Debug(bool logToFile) {
    00021   time_t timestamp;
    00022   if(logToFile) {
    00023     logFile.open("../Bin/Debug.log", ios::out);
    00024     if(!logToFile) {
    00025       // We can not open our log.
    00026       cerr << "Warning: Can not open Debug.log to write, continueing without logging\n";
    00027     } else {
    00028       // Log File is open, let us give it a nice time stamp.
    00029       timestamp = time(NULL);
    00030       logFile << "Log Started: " << ctime(&timestamp) << endl;
    00031     }
    00032   }
    00033 }
    00034
    00035 Debug::~Debug(void) {
    00036   time_t timestamp;
    00037
    00038   // We only need to close the log if it is open.
    00039   if(logFile) {
    00040     // Give it a closing timestamp.
    00041     timestamp = time(NULL);
    00042     logFile << endl << "Log Closed: " << ctime(&timestamp) << endl;
    00043
    00044     // Close the log file.
    00045     logFile.close();
    00046   }
    00047 }
    00048
    00049 void Debug::message(std::string msg) {
    00050   if(logFile) {
    00051     logFile << msg << endl;
    00052   }
    00053   cerr << msg << endl << endl;
    00054 }
    00055
    00056 void Debug::message(const char *msg, ...) {
    00057   va_list vargList; // This is to handlle the variable arguments
    00058
    00059   char outBuf[1024];
    00060   unsigned short outLen;
    00061
    00062   // This takes the arguments and puts them into the character array.
    00063   va_start(vargList, msg);
    00064
    00065 #if defined WIN32
    00066   outLen = _vsnprintf(outBuf, sizeof(outBuf), msg, vargList);
    00067 #else
    00068   outLen = vsnprintf(outBuf, sizeof(outBuf), msg, vargList);
    00069 #endif
    00070
    00071   va_end(vargList);
    00072
    00073   if(outLen >= sizeof(outBuf)) {
    00074     outLen = sizeof(outBuf);
    00075   }
    00076
    00077   if(logFile) {
    00078     logFile << outBuf << endl;
    00079   }
    00080
    00081   cerr << outBuf << endl;
    00082 }
    00083
    00084 bool Debug::openLog(bool logToFile) {
    00085   // Make sure the logger has not already been initialized.
    00086   if(logger != NULL) {
    00087     logger->message("Warning: Multiple calls to openLog().");
    00088     return false;
    00089   }
    00090   logger = new Debug(logToFile);
    00091   return true;
    00092 }
    00093
    00094 void Debug::closeLog(void) {
    00095   if(logger == NULL) {
    00096     cerr << "Warning: Call to closeLog() with NULL logger pointer." << endl;
    00097     return;
    00098   }
    00099   delete logger;
    00100   logger = NULL;
    00101 }

All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines