# log.cc

Go to the documentation of this file.

    00001 ///
    00002 /// \file       log.cc
    00003 ///             General Barry interface routines
    00004 ///
    00005
    00006 /*
    00007     Copyright (C) 2008-2011, Net Direct Inc. (http://www.netdirect.ca/)
    00008
    00009     This program is free software; you can redistribute it and/or modify
    00010     it under the terms of the GNU General Public License as published by
    00011     the Free Software Foundation; either version 2 of the License, or
    00012     (at your option) any later version.
    00013
    00014     This program is distributed in the hope that it will be useful,
    00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00017
    00018     See the GNU General Public License in the COPYING file at the
    00019     root directory of this project for more details.
    00020 */
    00021
    00022 #include "log.h"
    00023 #include "clog.h"
    00024 #include <pthread.h>
    00025 #include <stdio.h>
    00026 #include <stdarg.h>
    00027 #include <string.h>
    00028 #include <iostream>
    00029
    00030 namespace Barry {
    00031
    00032 extern bool __data_dump_mode__;
    00033 extern std::ostream *LogStream;
    00034 extern pthread_mutex_t LogStreamMutex;
    00035
    00036 LogLock::LogLock()
    00037 {
    00038         while( pthread_mutex_lock(&LogStreamMutex) != 0 )
    00039                 ;
    00040 }
    00041
    00042 LogLock::~LogLock()
    00043 {
    00044         pthread_mutex_unlock(&LogStreamMutex);
    00045 }
    00046
    00047
    00048 bool LogVerbose()
    00049 {
    00050         return __data_dump_mode__;
    00051 }
    00052
    00053 std::ostream* GetLogStream()
    00054 {
    00055         return LogStream;
    00056 }
    00057
    00058 } // namespace Barry
    00059
    00060 // Callable from C:
    00061
    00062 void BarryLogf(int verbose, const char *msg, ...)
    00063 {
    00064         va_list vl;
    00065         va_start(vl, msg);
    00066         char buffer[2048];
    00067         char *output = buffer;
    00068         int buflen = sizeof(buffer);
    00069         int n = vsnprintf(buffer, buflen, msg, vl);
    00070         if( n < 0 || n >= buflen ) {
    00071                 buflen = n + 100;
    00072                 output = new char [buflen];
    00073                 n = vsnprintf(output, buflen, msg, vl);
    00074                 if( n < 0 || n >= buflen ) {
    00075                         delete [] output;
    00076                         output = buffer;
    00077                         strcpy(buffer, "BarryLog: (trace error, output too long for buffer)");
    00078                 }
    00079         }
    00080         va_end(vl);
    00081
    00082         if( verbose ) {
    00083                 barryverbose(output);
    00084         }
    00085         else {
    00086                 barrylog(output);
    00087         }
    00088
    00089         if( output != buffer )
    00090                 delete [] output;
    00091 }
    00092