Classe avancée de log, thread safe et diverses surcharges

Description

Bonjour,

Je met à votre disposition cette classe de Log, thread safe, avec les possibilités qui me conviennent pour un projet.
Je n'ai pas trouvé une qui répondait à mes attentes alors j'ai décidé de me pencher la dessus et voila ce que ça donne.
Vous aurez besoin des bibliothèques boost.utility et boost.interprocess pour compiler, je les utilise principalement pour le mutex et la non-copie. Vous pouvez toujours endormir les parties mutex si vous n'avez pas boost, mais dans ce cas ce ne sera plus du thread safe.
Un petit "doxygen" dans le dossier vous générera une documentation complète.

Source / Exemple :


/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

  • /
#ifndef LOGGER_HEADER_BS #define LOGGER_HEADER_BS #include <boost/interprocess/sync/interprocess_mutex.hpp> #include <boost/interprocess/sync/scoped_lock.hpp> #include <boost/noncopyable.hpp> #include <iostream> #include <fstream> #include <ctime> #include <vector> #include <iomanip> namespace bs { /**
  • \class Logger
  • \author boli
  • \date 07/25/09
  • \file logger.hpp
  • \brief A thread safe class which manage a log file, using boost and the standard library
  • /
class Logger : private boost::noncopyable { public: /**
  • \brief Default constructor
  • \param a_fileName the file name to use
  • \param a_dateTimeFormat the format of date time to use, see documentaiton of strftime
  • \param a_useDateTime Tell to add the date time to logs or not
  • \param a_useEndLine Tell to add end line to logs or not
  • /
Logger(const std::string& a_fileName = std::string(), const std::string& a_dateTimeFormat = std::string("[%m/%d/%y - %H:%M] : "), const bool a_useDateTime = true, const bool a_useEndLine = false ); /**
  • \brief Change the the file name
  • \param a_fileName The new file name
  • /
bool setFileName(const std::string& a_fileName); /**
  • \brief Set the output of the logger to any ostream, std::cerr by default
  • \param os The output stream to use
  • /
void setOutputStream(std::ostream& os); /**
  • \brief Add a line to the log output
  • \param txt The text of the line to add
  • \return true is succesful, false other wise
  • /
bool append(const std::string& txt); /**
  • \brief Add a line to the log output
  • \param txt The text to add
  • \return The internal ostream object
  • /
std::ostream& operator<<(const std::string& txt); /**
  • \brief Overload which call std::ostream& operator<<(const std::string& txt) after conversion
  • /
std::ostream& operator<<(const char* cstyle); /**
  • \brief Overload which call std::ostream& operator<<(const std::string& txt) after conversion
  • /
std::ostream& operator<<(const int& nb); /**
  • \brief Overload which call std::ostream& operator<<(const std::string& txt) after conversion
  • /
std::ostream& operator<<(const double& real); /**
  • \brief Overload which call std::ostream& operator<<(const std::string& txt) after conversion
  • /
std::ostream& operator<<(const char& c); /**
  • \brief Overload which call std::ostream& operator<<(const std::string& txt) after conversion
  • /
std::ostream& operator<<(const unsigned char& uc); /**
  • \brief Overload which call std::ostream& operator<<(const std::string& txt) after conversion
  • /
std::ostream& operator<<(const void* ptr); /**
  • \brief Set the output format of the date and time, see the documentation of strftime for more info
  • default is: "[MM/DD/YY - HH:MM] : "
  • \param a_dateTimeFormat A string representing the desired format
  • /
void setDateTimeFormat(const std::string& a_dateTimeFormat); /**
  • \brief Tell to use the dateTime or not, default use it
  • \param use true to use the dateTime, false to not use it
  • /
void useDateTime(const bool use); /**
  • \brief Tell to automaticaly add the end of line (std::endl) or not, default don't use it
  • \param use True to use it, false to not
  • /
void useEndline(const bool use); /**
  • \brief Tell if the ouput stream is valid
  • \return true if the stream is valid (operator void*())
  • /
bool isValid(); /**
  • \brief Remove the log file
  • \return True if the file have been successfully removed
  • /
bool removeFile(); /**
  • \brief Rename the file and continue logging on the new file
  • \param newName the new name of the file
  • \return True if the file has been successfully renamed
  • /
bool renameFile(const std::string& newName); /**
  • \brief Reset the file, clear his content by erasing the old file
  • \return True if the file has been successfully reseted
  • /
bool resetFile(); /**
  • \brief Get the associated output stream of the logger
  • \return a reference to the std::ostream used
  • /
std::ostream& outputStream(); private: std::string dateTime(); /**
  • \brief Convert any type to std::string, see the documentation of std::ostringstream:operator<< for convertible types
  • \param The corresponding std::string
  • /
template<typename T> std::string convertToString(const T& elem) { std::ostringstream oss; oss << std::fixed << elem; return oss.str(); } private: std::string m_fileName; std::string m_dateTimeFormat; bool m_useDateTime; bool m_useEndLine; std::ofstream m_file; boost::interprocess::interprocess_mutex m_mutex; std::ostream *m_output; }; }; //end namespace bs #endif //end LOGGER_HEADER_BS

Conclusion :


Utilisez la et faites moi part de vos retours/suggestions.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.