# AuditFile.cc

Go to the documentation of this file.

    00001 // ----------------------------------------------------------------------------
    00002 // CERTI - HLA RunTime Infrastructure
    00003 // Copyright (C) 2002, 2003, 2004  ONERA
    00004 //
    00005 // This file is part of CERTI-libCERTI
    00006 //
    00007 // CERTI-libCERTI is free software ; you can redistribute it and/or
    00008 // modify it under the terms of the GNU Lesser General Public License
    00009 // as published by the Free Software Foundation ; either version 2 of
    00010 // the License, or (at your option) any later version.
    00011 //
    00012 // CERTI-libCERTI is distributed in the hope that it will be useful, but
    00013 // WITHOUT ANY WARRANTY ; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    00015 // Lesser General Public License for more details.
    00016 //
    00017 // You should have received a copy of the GNU Lesser General Public
    00018 // License along with this program ; if not, write to the Free Software
    00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
    00020 // USA
    00021 //
    00022 // $Id: AuditFile.cc,v 3.11 2007/07/06 09:25:18 erk Exp $
    00023 // ----------------------------------------------------------------------------
    00024
    00025
    00026 #include "AuditFile.hh"
    00027
    00028 #include <iostream>
    00029 #include <cstdarg>
    00030 #include <sstream>
    00031
    00032 using std::ofstream ;
    00033 using std::ios ;
    00034 using std::cerr ;
    00035 using std::endl ;
    00036 using std::string ;
    00037
    00038 namespace certi {
    00039
    00040 // ----------------------------------------------------------------------------
    00042
    00044 AuditFile::AuditFile(const std::string logfile)
    00045     : auditFile(logfile.c_str(), ios::app)
    00046 {
    00047     if (!auditFile.is_open()) {
    00048         cerr << "Could not open Audit file � " << logfile.c_str()
    00049          << " �." << endl ;
    00050         throw RTIinternalError("Could not open Audit file.");
    00051     }
    00052
    00053     // Put a Start delimiter in the Audit File
    00054     putLine(AUDITEVENT_START_AUDIT, AUDIT_MAX_LEVEL, e_NO_EXCEPTION, "");
    00055 }
    00056
    00057 // ----------------------------------------------------------------------------
    00059
    00062 AuditFile::~AuditFile()
    00063 {
    00064     endLine(e_NO_EXCEPTION, "");
    00065     putLine(AUDITEVENT_STOP_AUDIT, AUDIT_MAX_LEVEL, e_NO_EXCEPTION, "");
    00066     auditFile.close();
    00067 }
    00068
    00069 // ----------------------------------------------------------------------------
    00071
    00074 void
    00075 AuditFile::endLine(unsigned short event_status, std::string reason)
    00076 {
    00077     if (currentLine.started())
    00078     currentLine.end(event_status, reason);
    00079
    00080     // Log depending on level and non-zero status.
    00081     if (currentLine.getLevel() >= AUDIT_CURRENT_LEVEL ||
    00082     currentLine.getStatus())
    00083     currentLine.write(auditFile);
    00084
    00085     currentLine = AuditLine();
    00086 }
    00087
    00088 // ----------------------------------------------------------------------------
    00090 // void
    00091 // AuditFile::addToLine(const string comment)
    00092 // {
    00093 //     currentLine.addComment(comment);
    00094 // }
    00095
    00096 // ----------------------------------------------------------------------------
    00098 // void
    00099 // AuditFile::addToLinef(const char *Format, ...)
    00100 // {
    00101 //     va_list argptr ; // Variable Argument list, see cstdarg
    00102
    00103 //     if ((currentLine != NULL) && (Format != NULL)) {
    00104 //         va_start(argptr, Format);
    00105 //         vsprintf(va_Buffer, Format, argptr);
    00106 //         va_end(argptr);
    00107
    00108 //         currentLine->addComment(va_Buffer);
    00109 //     }
    00110 // }
    00111
    00112 // ----------------------------------------------------------------------------
    00114
    00119 void
    00120 AuditFile::putLine(unsigned short event_type,
    00121                    unsigned short event_level,
    00122                    unsigned short event_status,
    00123                    std::string reason)
    00124 {
    00125     if (event_level >= AUDIT_CURRENT_LEVEL) {
    00126     AuditLine line(event_type, event_level, event_status, reason);
    00127     line.write(auditFile);
    00128     }
    00129 }
    00130
    00131 // ----------------------------------------------------------------------------
    00133 void
    00134 AuditFile::startLine(Handle federation,
    00135                      FederateHandle federate,
    00136                      unsigned short event_type)
    00137 {
    00138     // Check already valid opened line
    00139     if (currentLine.started()) {
    00140         cerr << "Audit Error : Current line already valid !" << endl ;
    00141         return ;
    00142     }
    00143
    00144     currentLine = AuditLine(event_type, AUDIT_MIN_LEVEL, 0, "");
    00145     currentLine.setFederation(federation);
    00146     currentLine.setFederate(federate);
    00147 }
    00148
    00149 // ----------------------------------------------------------------------------
    00151
    00154 void
    00155 AuditFile::setLevel(unsigned short eventLevel)
    00156 {
    00157     currentLine.setLevel(eventLevel);
    00158 }
    00159
    00160 // ----------------------------------------------------------------------------
    00163 AuditFile &
    00164 AuditFile::operator<<(const char *s)
    00165 {
    00166     if (s != 0)
    00167     currentLine.addComment(s);
    00168     return *this ;
    00169 }
    00170
    00171 AuditFile &
    00172 AuditFile::operator<<(int n)
    00173 {
    00174     std::ostringstream s ;
    00175     s << n ;
    00176     currentLine.addComment(s.str());
    00177     return *this ;
    00178 }
    00179
    00180 AuditFile &
    00181 AuditFile::operator<<(long n)
    00182 {
    00183     std::ostringstream s ;
    00184     s << n ;
    00185     currentLine.addComment(s.str());
    00186     return *this ;
    00187 }
    00188
    00189 AuditFile &
    00190 AuditFile::operator<<(unsigned int n)
    00191 {
    00192     std::ostringstream s ;
    00193     s << n ;
    00194     currentLine.addComment(s.str());
    00195     return *this ;
    00196 }
    00197
    00198 AuditFile &
    00199 AuditFile::operator<<(unsigned long n)
    00200 {
    00201     std::ostringstream s ;
    00202     s << n ;
    00203     currentLine.addComment(s.str());
    00204     return *this ;
    00205 }
    00206
    00207 AuditFile &
    00208 AuditFile::operator<<(double n)
    00209 {
    00210     std::ostringstream s ;
    00211     s << n ;
    00212     currentLine.addComment(s.str());
    00213     return *this ;
    00214 }
    00215
    00216 }
    00217
    00218 // $Id: AuditFile.cc,v 3.11 2007/07/06 09:25:18 erk Exp $