Back to index

nordugrid-arc-nox  1.1.0~rc6
Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes
Arc::LogFile Class Reference

A class for logging to files. More...

#include <Logger.h>

Inheritance diagram for Arc::LogFile:
Inheritance graph
[legend]
Collaboration diagram for Arc::LogFile:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 LogFile (const std::string &path)
 Creates a LogFile connected to a file.
 LogFile (const std::string &path, const std::string &locale)
 Creates a LogFile connected to a file.
void setMaxSize (int newsize)
 Set maximal allowed size of file.
void setBackups (int newbackup)
 Set number of backups to store.
 operator bool (void)
 Returns true if this instance is valid.
bool operator! (void)
 Returns true if this instance is invalid.
virtual void log (const LogMessage &message)
 Writes a LogMessage to the file.
void setFormat (const LogFormat &newformat)

Protected Attributes

std::string locale
LogFormat format

Private Member Functions

 LogFile (void)
 LogFile (const LogFile &unique)
void operator= (const LogFile &unique)
void backup (void)

Private Attributes

std::string path
std::ofstream destination
int maxsize
int backups
Glib::Mutex mutex

Detailed Description

A class for logging to files.

This class is used for logging to files. It provides synchronization in order to prevent different LogMessages to appear mixed with each other in the stream. It is possible to limit size of created file. Whenever specified size is exceeded fiel is deleted and new one is created. Old files may be moved into backup files instead of being deleted. Those files have names same as initial file with additional number suffix - similar to those found in /var/log of many Unix-like systems.

Definition at line 303 of file Logger.h.


Constructor & Destructor Documentation

Arc::LogFile::LogFile ( const std::string &  path)

Creates a LogFile connected to a file.

Creates a LogFile connected to the file located at specified path. In order not to break synchronization, it is important not to connect more than one LogFile object to a certain file. If file does not exist it will be created.

Parameters:
pathThe path to file to which to write LogMessages.

Definition at line 232 of file Logger.cpp.

    : LogDestination(),
      path(path),
      destination(),
      maxsize(-1),
      backups(-1) {
    if(path.empty()) {
      //logger.msg(Arc::ERROR,"Log file path is not specified");
      return;
    }
    destination.open(path.c_str(), std::fstream::out | std::fstream::app);
    if(!destination.is_open()) {
      //logger.msg(Arc::ERROR,"Failed to open log file: %s",path);
      return;
    }
  }
Arc::LogFile::LogFile ( const std::string &  path,
const std::string &  locale 
)

Creates a LogFile connected to a file.

Creates a LogFile connected to the file located at specified path. The output will be localised to the specified locale.

Definition at line 249 of file Logger.cpp.

    : LogDestination(locale),
      path(path),
      destination(),
      maxsize(-1),
      backups(-1) {
    if(path.empty()) {
      //logger.msg(Arc::ERROR,"Log file path is not specified");
      return;
    }
    destination.open(path.c_str(), std::fstream::out | std::fstream::app);
    if(!destination.is_open()) {
      //logger.msg(Arc::ERROR,"Failed to open log file: %s",path);
      return;
    }
  }
Arc::LogFile::LogFile ( void  ) [private]

Definition at line 274 of file Logger.cpp.

    : LogDestination(), maxsize(-1), backups(-1) {
    // Executing this code should be impossible!
    exit(EXIT_FAILURE);
  }
Arc::LogFile::LogFile ( const LogFile unique) [private]

Definition at line 280 of file Logger.cpp.

    : LogDestination(), maxsize(-1), backups(-1) {
    // Executing this code should be impossible!
    exit(EXIT_FAILURE);
  }

Member Function Documentation

void Arc::LogFile::backup ( void  ) [private]

Definition at line 313 of file Logger.cpp.

                           {
    if(maxsize <= 0) return;
    if(destination.tellp() < maxsize) return;
    bool backup_done = true;
    // Not sure if this will work on windows, but glibmm
    // has no functions for removing and renaming files
    if(backups > 0) {
      std::string backup_path = path+"."+tostring(backups);
      ::unlink(backup_path.c_str());
      for(int n = backups;n>0;--n) {
        std::string old_backup_path = (n>1)?(path+"."+tostring(n-1)):path;
        if(::rename(old_backup_path.c_str(),backup_path.c_str()) != 0) {
          if(n == 1) backup_done=false;
        }
        backup_path = old_backup_path;
      }
    } else {
      if(::unlink(path.c_str()) != 0) backup_done=false;
    }
    if(backup_done) {
      destination.close();
      destination.open(path.c_str(), std::fstream::out | std::fstream::app);
    }
  }

Here is the call graph for this function:

Here is the caller graph for this function:

void Arc::LogFile::log ( const LogMessage message) [virtual]

Writes a LogMessage to the file.

This method writes a LogMessage to the file that is connected to this LogFile object. If after writitng size of file exceeds one set by setMaxSize() file is moved to backup and new one is created.

Parameters:
messageThe LogMessage to write.

Implements Arc::LogDestination.

Definition at line 300 of file Logger.cpp.

                                             {
    Glib::Mutex::Lock lock(mutex);
    if(!destination.is_open()) return;
    const char *loc = NULL;
    if (!locale.empty()) {
      loc = setlocale(LC_ALL, NULL);
      setlocale(LC_ALL, locale.c_str());
    }
    destination << LoggerFormat(format) << message << std::endl;
    if (!locale.empty()) setlocale(LC_ALL, loc);
    backup();
  }

Here is the call graph for this function:

Arc::LogFile::operator bool ( void  )

Returns true if this instance is valid.

Definition at line 290 of file Logger.cpp.

                             {
    Glib::Mutex::Lock lock(mutex);
    return destination.is_open();
  }
bool Arc::LogFile::operator! ( void  )

Returns true if this instance is invalid.

Definition at line 295 of file Logger.cpp.

                              {
    Glib::Mutex::Lock lock(mutex);
    return !destination.is_open();
  }
void Arc::LogFile::operator= ( const LogFile unique) [private]

Definition at line 286 of file Logger.cpp.

                                        {
    // Executing this code should be impossible!
    exit(EXIT_FAILURE);
  }
void Arc::LogFile::setBackups ( int  newbackup)

Set number of backups to store.

Set number of backups to store. When file size exceeds one specified with setMaxSize() file is closed and moved to one named path.1. If path.1 exists it is moved to path.2 and so on. Number of path.# files is one set in newbackup.

Parameters:
newbackupNumber of backup files.

Definition at line 270 of file Logger.cpp.

                                         {
    backups = newbackups;
  }

Here is the caller graph for this function:

void Arc::LogDestination::setFormat ( const LogFormat newformat) [inherited]

Definition at line 197 of file Logger.cpp.

                                                           {
    format = newformat;
  }

Here is the caller graph for this function:

void Arc::LogFile::setMaxSize ( int  newsize)

Set maximal allowed size of file.

Set maximal allowed size of file. This value is not obeyed exactly. Spesified size may be exceeded by amount of one LogMessage. To disable limit specify -1.

Parameters:
newsizeMax size of log file.

Definition at line 266 of file Logger.cpp.

                                      {
    maxsize = newsize;
  }

Here is the caller graph for this function:


Member Data Documentation

int Arc::LogFile::backups [private]

Definition at line 362 of file Logger.h.

std::ofstream Arc::LogFile::destination [private]

Definition at line 360 of file Logger.h.

LogFormat Arc::LogDestination::format [protected, inherited]

Definition at line 222 of file Logger.h.

std::string Arc::LogDestination::locale [protected, inherited]

Definition at line 221 of file Logger.h.

int Arc::LogFile::maxsize [private]

Definition at line 361 of file Logger.h.

Glib::Mutex Arc::LogFile::mutex [private]

Definition at line 363 of file Logger.h.

std::string Arc::LogFile::path [private]

Definition at line 359 of file Logger.h.


The documentation for this class was generated from the following files: