GLog.hpp 14.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
/***************************************************************************
 *                       GLog.hpp - Information logger                     *
 * ----------------------------------------------------------------------- *
 *  copyright (C) 2010-2013 by Juergen Knoedlseder                         *
 * ----------------------------------------------------------------------- *
 *                                                                         *
 *  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/>.  *
 *                                                                         *
 ***************************************************************************/
/**
 * @file GLog.hpp
 * @brief Information logger class definition
 * @author Juergen Knoedlseder
 */

#ifndef GLOG_HPP
#define GLOG_HPP

/* __ Includes ___________________________________________________________ */
#include <string>
#include <iostream>
#include "GTypemaps.hpp"

/* __ Forward declarations _______________________________________________ */


/***********************************************************************//**
 * @class GLog
 *
 * @brief Information logger interface definition.
 *
 * This class implements an interface for logging of text messages to the
 * standard output and/or error streams and into an ASCII log file. It
 * provides a C++ style interface (allowing to stream messages using code
 * such as
 *
 *     log << "This is a message";
 *
 * and a C style interface that allows making use of C style text formating
 * using code such as
 *
 *     log("These are %d messages with %d items.", n, m);
 *
 * The logger interface implements an internal character buffer that is
 * flushed once a maximum size parameter is exceeded. The buffer limit can
 * be modified using the max_size() method; by default it is set to 8192
 * characters. The logger allows prepending of the current date and the task
 * name.
 ***************************************************************************/
class GLog {

public:
    // Constructors and destructors
    GLog(void);
    GLog(const std::string& filename, const bool& clobber = false);
    GLog(const GLog& log);
    virtual ~GLog(void);

    // Operators
    GLog& operator=(const GLog& log);
    void  operator()(const char *msgFormat, ...);
    GLog& operator<<(GLog& log);
    GLog& operator<<(const std::string& str);
    GLog& operator<<(const char* str);
    GLog& operator<<(const char& value);
    GLog& operator<<(const unsigned char& value);
    GLog& operator<<(const bool& value);
    GLog& operator<<(const int& value);
    GLog& operator<<(const unsigned int& value);
    GLog& operator<<(const double& value);
    GLog& operator<<(std::ostream& (*fn)(std::ostream&));

    // Methods
    void               clear(void);
    int                size(void) const;
    std::string        classname(void) const;
    void               open(const std::string& filename, const bool& clobber = false);
    void               close(void);
    void               flush(const bool& force = false);
    void               date(const bool& flag);
    void               cout(const bool& flag);
    void               cerr(const bool& flag);
    void               name(const std::string& name);
    void               max_size(const int& size);
    void               indent(const int& indent);
    void               chatter(const GChatter& chatter);
    void               header0(const std::string& arg);
    void               header1(const std::string& arg);
    void               header2(const std::string& arg);
    void               header3(const std::string& arg);
    const bool&        date(void) const;
    const bool&        cout(void) const;
    const bool&        cerr(void) const;
    const std::string& name(void) const;
    const int&         max_size(void) const;
    const int&         indent(void) const;
    const GChatter&    chatter(void) const;
    const std::string& filename(void) const;

protected:
    // Protected methods
    void        init_members(void);
    void        copy_members(const GLog& log);
    void        free_members(void);
    void        header(const std::string& arg, int level);
    std::string strdate(void) const;
    std::string prefix(void) const;
    void        append(std::string arg);

    // Protected data members
    int         m_max_length; //!< Maximum buffer length
    int         m_indent;     //!< Indentation of text
    bool        m_stdout;     //!< Dump in standard output
    bool        m_stderr;     //!< Dump in standard error
    bool        m_use_date;   //!< Dump date in prefix
    bool        m_linestart;  //!< Signals that buffer is at line start
    FILE*       m_file;       //!< Log file pointer
    std::string m_filename;   //!< Log file name
    std::string m_name;       //!< Name for prefix
    std::string m_buffer;     //!< Output string buffer
    GChatter    m_chatter;    //!< Chattiness for print() method
};


/***********************************************************************//**
 * @brief Return class name
 *
 * @return String containing the class name ("GLog").
 ***************************************************************************/
inline
std::string GLog::classname(void) const
{
    return ("GLog");
}


/***********************************************************************//**
 * @brief Write string as centred header into logger
 *
 * @param[in] arg Header string.
 *
 * Writes a string as 80 character wide centred header.
 ***************************************************************************/
inline
void GLog::header0(const std::string& arg)
{
    header(arg, 0);
    return;
}


/***********************************************************************//**
 * @brief Write string as header framed by '=' characters into logger
 *
 * @param[in] arg Header string.
 *
 * Writes a string as header framed by '=' characters into logger.
 ***************************************************************************/
inline
void GLog::header1(const std::string& arg)
{
    header(arg, 1);
    return;
}


/***********************************************************************//**
 * @brief Write string as header framed by '-' characters into logger
 *
 * @param[in] arg Header string.
 *
 * Writes a string as header framed by '-' characters into logger.
 ***************************************************************************/
inline
void GLog::header2(const std::string& arg)
{
    header(arg, 2);
    return;
}


/***********************************************************************//**
 * @brief Write string as header enclosed by '===' into logger
 *
 * @param[in] arg Header string.
 *
 * Writes a string as header enclosed by '===' into logger.
 ***************************************************************************/
inline
void GLog::header3(const std::string& arg)
{
    header(arg, 3);
    return;
}


/***********************************************************************//**
 * @brief Set date flag that controls date prefixing
 *
 * @param[in] flag Enable/disable date flag (true/false). 
 *
 * The date flag specifies whether each new line will be prepended by the
 * actual date when the writing of the line was started.
 ***************************************************************************/
inline
void GLog::date(const bool& flag)
{
    m_use_date = flag;
    return;
}


/***********************************************************************//**
 * @brief Set standard output stream (cout) flag
 *
 * @param[in] flag Enable/disable logging (true/false).
 *
 * The cout flag specifies whether the logging should go in addition to the
 * log file also into the standard output stream.
 ***************************************************************************/
inline
void GLog::cout(const bool& flag)
{
    m_stdout = flag;
    return;
}


/***********************************************************************//**
 * @brief Enables/disables logging into standard error stream
 *
 * @param[in] flag Enable/disable logging (true/false).
 *
 * The cerr flag specifies whether the logging should go in addition to the
 * log file also into the standard error stream.
 ***************************************************************************/
inline
void GLog::cerr(const bool& flag)
{
    m_stderr = flag;
    return;
}


/***********************************************************************//**
 * @brief Set name to put into prefix
 *
 * @param[in] name Name to put into prefix. 
 *
 * Sets the @p name that will be prefixed to each new line. If the date flag
 * is true, the @p name will be inserted after the date at the beginning of
 * each line.
 *
 * Specifying an empty string will suppress the insertion of the name into
 * the logger.
 ***************************************************************************/
inline
void GLog::name(const std::string& name)
{
    m_name = name;
    return;
}


/***********************************************************************//**
 * @brief Set the maximum buffer size
 *
 * @param[in] size Maximum buffer size.
 *
 * Set the maximum number of characters allowed in the internal buffer before
 * the buffer is flushed into the specified streams or file.
 *
 * @todo No maximum buffer size checking is performed. This could be
 * implemented but is not really crucial since the physical buffer size can
 * indeed be larger than this maximum size.
 ***************************************************************************/
inline
void GLog::max_size(const int& size)
{
    m_max_length = size;
    return;
}


/***********************************************************************//**
 * @brief Set indentation
 *
 * @param[in] indent Number of indentation characters.
 *
 * Sets the number of whitespace characters that are added after the
 * prefix and before the log message content for each line.
 *
 * Indentation allows for easy formatting of text in the log file.
 ***************************************************************************/
inline
void GLog::indent(const int& indent)
{
    m_indent = indent;
    return;
}


/***********************************************************************//**
 * @brief Set chattiness
 *
 * @param[in] chatter Chattiness.
 *
 * Sets the chattiness that is applied to print() methods that are piped into
 * the logger. The following chattiness values are available:
 * - SILENT: no logging will be performed
 * - TERSE: minimum logging
 * - NORMAL: normal level of logging
 * - EXPLICIT: detailed information will be logged
 * - VERBOSE: maximum information will be logged
 ***************************************************************************/
inline
void GLog::chatter(const GChatter& chatter)
{
    m_chatter = chatter;
    return;
}


/***********************************************************************//**
 * @brief Return date flag
 *
 * @return True if date flag is set.
 *
 * The date flag specifies whether each new line will be prepended by the
 * actual date when the writing of the line was started.
 ***************************************************************************/
inline
const bool& GLog::date(void) const
{
    return m_use_date;
}


/***********************************************************************//**
 * @brief Return standard output stream (cout) flag
 *
 * @return True if cout flag is set.
 *
 * The cout flag specifies whether the logging should go in addition to the
 * log file also into the standard output stream.
 ***************************************************************************/
inline
const bool& GLog::cout(void) const
{
    return m_stdout;
}


/***********************************************************************//**
 * @brief Return standard error stream (cerr) flag
 *
 * @return True if cerr flag is set.
 *
 * The cerr flag specifies whether the logging should go in addition to the
 * log file also into the standard error stream.
 ***************************************************************************/
inline
const bool& GLog::cerr(void) const
{
    return m_stderr;
}


/***********************************************************************//**
 * @brief Return prefix name
 *
 * @return Name to put into prefix.
 *
 * Returns the name that will be prefixed to each new line. If the name is
 * empty, nothing will be prefixed.
 ***************************************************************************/
inline
const std::string& GLog::name(void) const
{
    return m_name;
}


/***********************************************************************//**
 * @brief Return the maximum buffer size
 *
 * @return Maximum buffer size.
 *
 * Returns the maximum number of characters allowed in the internal buffer
 * before the buffer is flushed into the specified streams or file.
 ***************************************************************************/
inline
const int& GLog::max_size(void) const
{
    return m_max_length;
}


/***********************************************************************//**
 * @brief Return indentation
 *
 * @return Number of indentation characters.
 *
 * Returns the number of whitespace characters that are added after the
 * prefix and before the log message content for each line.
 ***************************************************************************/
inline
const int& GLog::indent(void) const
{
    return m_indent;
}


/***********************************************************************//**
 * @brief Return chattiness
 *
 * @return Chattiness.
 *
 * Returns the chattiness that is applied to print() methods that are piped
 * into the logger. The following chattiness values are available:
 * - SILENT: no logging will be performed
 * - TERSE: minimum logging
 * - NORMAL: normal level of logging
 * - EXPLICIT: detailed information will be logged
 * - VERBOSE: maximum information will be logged
 ***************************************************************************/
inline
const GChatter& GLog::chatter(void) const
{
    return m_chatter;
}


/***********************************************************************//**
 * @brief Return log filename
 *
 * @return Log filename.
 *
 * Returns the filename of the log file.
 ***************************************************************************/
inline
const std::string& GLog::filename(void) const
{
    return m_filename;
}

#endif /* GLOG_HPP */