GLog.cpp 23.3 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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
/***************************************************************************
 *                       GLog.cpp - Information logger                     *
 * ----------------------------------------------------------------------- *
 *  copyright (C) 2010-2014 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.cpp
 * @brief Information logger class implementation
 * @author Juergen Knoedlseder
 */

/* __ Includes ___________________________________________________________ */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctime>
#include <cstdarg>      // for std::va_list type
#include <cstdio>       // std::fopen, std::fgets, std::fclose, etc...
#include <cstring>      // std::memcpy
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include "GLog.hpp"
#include "GTools.hpp"

/* __ Method name definitions ____________________________________________ */

/* __ Macros _____________________________________________________________ */

/* __ Coding definitions _________________________________________________ */

/* __ Debug definitions __________________________________________________ */


/*==========================================================================
 =                                                                         =
 =                         Constructors/destructors                        =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Void constructor
 ***************************************************************************/
GLog::GLog(void)
{
    // Initialise private members for clean destruction
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Log file constructor
 *
 * @param[in] filename Name of log file.
 * @param[in] clobber If true overwrite any existing file, otherwise append.
 *
 * Construct a logger with an open log file.
 ***************************************************************************/
GLog::GLog(const std::string& filename, const bool& clobber)
{
    // Initialise private members for clean destruction
    init_members();

    // Open file
    open(filename, clobber);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy constructor
 *
 * @param[in] log Object from which the instance should be built.
 ***************************************************************************/
GLog::GLog(const GLog& log)
{ 
    // Initialise private members for clean destruction
    init_members();

    // Copy members
    copy_members(log);

    // Return
    return;
}


/***********************************************************************//**
 * @brief Destructor
 ***************************************************************************/
GLog::~GLog(void)
{
    // Free members
    free_members();

    // Return
    return;
}


/*==========================================================================
 =                                                                         =
 =                                Operators                                =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Assignment operator
 *
 * @param[in] log Object which should be assigned.
 ***************************************************************************/
GLog& GLog::operator= (const GLog& log)
{ 
    // Execute only if object is not identical
    if (this != &log) {

        // Free members
        free_members();

        // Initialise private members for clean destruction
        init_members();

        // Copy members
        copy_members(log);

    } // endif: object was not identical
  
    // Return
    return *this;
}


/**************************************************************************//**
 * @brief Message logging operator
 *
 * @param[in] msgFormat Pointer to message format.
 * @param[in] ... Message to put into log.
 *
 * This operator provides message logging capabilities in C style. It expects
 * a C-style format string followed by an arbitrary number of arguments that
 * is used to fill the format.
 *
 * @todo A static buffer of length 8192 is allocated. Although this is likely
 * sufficient for any normal application there is nothing that prevents in
 * principle the overflow of the buffer. A more sophisticated scheme should be
 * implemented.
 ******************************************************************************/
void GLog::operator()(const char *msgFormat, ...)
{
    // Allocate message buffer
    char buffer[8192];

    // Put message into buffer
    std::va_list vl;
    va_start(vl, msgFormat);
    std::vsprintf(buffer, msgFormat, vl);
    va_end(vl);

    // Append message to string buffer
    append(std::string(buffer)+"\n");

    // Return
    return;

}


/***********************************************************************//**
 * @brief Insert logger into logger
 *
 * @param[in] log Logger to be inserted.
 *
 * Insert a logger object into another logger object. This method handles
 * information that has either been written into a file, or that is still
 * present in the logger buffer.
 ***************************************************************************/
GLog& GLog::operator<<(GLog& log)
{
    // If log object has an open file then append the log buffer to this
    // buffer
    if (log.m_file == NULL) {
        m_buffer.append(log.m_buffer);
    }

    // ... otherwise read all information from file and append it to the
    // buffer
    else {

        // Force flush so that the log object writes the entire buffer
        // into the file
        log.flush(true);

        // Get the filename
        std::string filename = log.filename();

        // Close log's file
        log.close();

        // Open log's file in read mode
        std::ifstream file(filename.c_str(), std::ios::in);
        if (file) {

            // Append all lines from file to this buffer
            std::string line;
            while(std::getline(file,line)){
                m_buffer.append(line+"\n");
            }

            // Close log's file
            file.close();
        }

        // Open log's file in append mode
        log.open(filename,false);
    }

    // Flush
    flush();

    // Return logger
    return *this;
}


/***********************************************************************//**
 * @brief Insert C++ string into logger
 *
 * @param[in] str C++ string to be inserted.
 *
 * This method insert a C++ string into the logger.
 ***************************************************************************/
GLog& GLog::operator<<(const std::string& str)
{
    // Add string to buffer
    append(str);

    // Return logger
    return *this;
}


/***********************************************************************//**
 * @brief Insert C string into logger
 *
 * @param[in] str C string to be inserted.
 *
 * This method insert a C string into the logger.
 ***************************************************************************/
GLog& GLog::operator<<(const char* str)
{
    // Add string to buffer
    append(std::string(str));

    // Return logger
    return *this;
}


/***********************************************************************//**
 * @brief Insert character value into logger
 *
 * @param[in] value Character value to be inserted.
 *
 * This method inserts a character value into the logger.
 ***************************************************************************/
GLog& GLog::operator<<(const char& value)
{
    // Allocate output stream
    std::ostringstream oss;

    // Put value into stream
    oss << value;

    // Append stream to buffer
    append(oss.str());

    // Return logger
    return *this;
}


/***********************************************************************//**
 * @brief Insert unsigned character value into logger
 *
 * @param[in] value Unsigned character value to be inserted.
 *
 * This method inserts an unsigned character value into the logger.
 ***************************************************************************/
GLog& GLog::operator<<(const unsigned char& value)
{
    // Allocate output stream
    std::ostringstream oss;

    // Put value into stream
    oss << value;

    // Append stream to buffer
    append(oss.str());

    // Return logger
    return *this;
}


/***********************************************************************//**
 * @brief Insert bool into logger
 *
 * @param[in] value Boolean to be inserted.
 *
 * Depending on the argument, this method inserts either the character string
 * "true" or "false" into the logger.
 ***************************************************************************/
GLog& GLog::operator<<(const bool& value)
{
    // Add boolean to buffer
    if (value) {
        append("true");
    }
    else {
        append("false");
    }

    // Return logger
    return *this;
}


/***********************************************************************//**
 * @brief Insert integer into logger
 *
 * @param[in] value Integer to be inserted.
 *
 * This method inserts an integer into the logger.
 ***************************************************************************/
GLog& GLog::operator<<(const int& value)
{
    // Allocate output stream
    std::ostringstream oss;

    // Put value into stream
    oss << value;

    // Append stream to buffer
    append(oss.str());

    // Return logger
    return *this;
}


/***********************************************************************//**
 * @brief Insert unsigned integer into logger
 *
 * @param[in] value Unsigned integer to be inserted.
 *
 * This method inserts an unsigned integer into the logger.
 ***************************************************************************/
GLog& GLog::operator<<(const unsigned int& value)
{
    // Allocate output stream
    std::ostringstream oss;

    // Put value into stream
    oss << value;

    // Append stream to buffer
    append(oss.str());

    // Return logger
    return *this;
}


/***********************************************************************//**
 * @brief Insert double precision value into logger
 *
 * @param[in] value Double precision value to be inserted.
 *
 * This method inserts a double precision value into the logger.
 ***************************************************************************/
GLog& GLog::operator<<(const double& value)
{
    // Allocate output stream
    std::ostringstream oss;

    // Put value into stream
    oss << value;

    // Append stream to buffer
    append(oss.str());

    // Return logger
    return *this;
}


/***********************************************************************//**
 * @brief Ostream function output operator
 *
 * @param[in] fn Ostream function.
 *
 * The function output operator is interpreted as a line feed operator.
 *
 * @todo This is a quick an dirty implementation of the std::endl operator.
 * A clean GLog::endl operator should be implemented instead, yet this needs
 * some deeper understanding of our the 
 ***************************************************************************/
GLog& GLog::operator<<(std::ostream& (*fn)(std::ostream&))
{
    // Append CR to buffer
    append("\n");

    // Return logger
    return *this;
}


/*==========================================================================
 =                                                                         =
 =                             Public methods                              =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Clear object
 ***************************************************************************/
void GLog::clear(void)
{
    // Free members
    free_members();

    // Init members
    init_members();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Return the number of characters that are actually in the buffer
 ***************************************************************************/
int GLog::size(void) const
{
    // Return
    return m_buffer.size();
}


/***********************************************************************//**
 * @brief Open log file
 *
 * @param[in] filename Name of log file.
 * @param[in] clobber If true overwrite any existing file, otherwise append.
 *
 * Opens a file for logging. If a log file was already open it is closed
 * before opening a new file.
 ***************************************************************************/
void GLog::open(const std::string& filename, const bool& clobber)
{
    // Store the filename
    m_filename = filename;

    // Close any existing file
    close();

    // Open file
    if (clobber) {
        m_file = std::fopen(filename.c_str(), "w");
    }
    else {
        m_file = std::fopen(filename.c_str(), "a");
    }

    // Return
    return;
}


/***********************************************************************//**
 * @brief Close log file
 *
 * Close log file. This method even works if no file has been opened. In any
 * case flushing of the buffer is enforced.
 ***************************************************************************/
void GLog::close(void)
{
    // Flush buffer
    flush(true);

    // Close any open file
    if (m_file != NULL) {
        std::fclose(m_file);
        m_file = NULL;
    }

    // Return
    return;
}


/*==========================================================================
 =                                                                         =
 =                             Private methods                             =
 =                                                                         =
 ==========================================================================*/

/***********************************************************************//**
 * @brief Initialise class members
 ***************************************************************************/
void GLog::init_members(void)
{
    // Initialise members
    m_max_length = 8192;
    m_indent     = 0;
    m_stdout     = false;
    m_stderr     = false;
    m_use_date   = false;
    m_linestart  = true;
    m_file       = NULL;
    m_filename.clear();
    m_name.clear();
    m_buffer.clear();
    m_chatter    = NORMAL;

    // Return
    return;
}


/***********************************************************************//**
 * @brief Copy class members
 *
 * @param[in] log Object from which members which should be copied.
 ***************************************************************************/
void GLog::copy_members(const GLog& log)
{
    // Copy attributes
    m_max_length = log.m_max_length;
    m_indent     = log.m_indent;
    m_stdout     = log.m_stdout;
    m_stderr     = log.m_stderr;
    m_use_date   = log.m_use_date;
    m_linestart  = log.m_linestart;
    m_file       = log.m_file;
    m_filename   = log.m_filename;
    m_name       = log.m_name;
    m_buffer     = log.m_buffer;
    m_chatter    = log.m_chatter;

    // Return
    return;
}


/***********************************************************************//**
 * @brief Delete class members
 *
 * Deletes class member after flushing the buffer and closing the log file.
 ***************************************************************************/
void GLog::free_members(void)
{
    // Flush buffer
    flush(true);

    // Close log file
    close();

    // Return
    return;
}


/***********************************************************************//**
 * @brief Flush string buffer into log file
 *
 * @param[in] force If true, force flushing.
 *
 * Flush string buffer if it is full or if flushing is enforced. This method
 * writes the output string in the relevant streams. It decomposes the
 * buffer in lines that a separated by a '\n' character. The following
 * streams are currently implemented (and will be filled in parallel):
 * stdout, stderr, and an ASCII file.
 ***************************************************************************/
void GLog::flush(const bool& force)
{
    // Check if buffer should be flushed
    bool flush = (force || size() > m_max_length);

    // Flush buffer
    if (flush) {

        // Flush buffer until it is empty
        while (m_buffer.size() > 0) {

            // Find next CR
            std::string line;
            std::size_t pos = m_buffer.find_first_of("\n", 0);
            if (pos == std::string::npos) {
                line        = m_buffer;
                m_linestart = false;
                m_buffer.clear();
            }
            else {
                line        = m_buffer.substr(0, pos) + "\n";
                m_buffer    = m_buffer.substr(pos+1, m_buffer.size()-pos);
                m_linestart = true;
            }

            // Put line into file
            if (m_file != NULL) {
                std::fprintf(m_file, "%s", line.c_str());
            }

        } // endwhile: flush until empty

    } // endif: flush was required

    // Return
    return;
}


/***********************************************************************//**
 * @brief Write string as header into logger
 *
 * @param[in] arg Header string.
 * @param[in] level Header level (0,1,2,3).
 *
 * Writes a string as header into the logger. Various header levels exist:
 * 0: 80 character wide centred header
 * 1: Header framed by '=' characters
 * 2: Header framed by '-' characters
 * 3: Header enclosed by '===' string
 ***************************************************************************/
void GLog::header(const std::string& arg, int level)
{
    // Declare frame and text strings
    std::string frame;
    std::string text;

    // Create level dependent header strings
    switch (level) {
    case 0:
        break;
    case 1:
    case 2:
        text  = "| " + arg + " |";
        frame = (level == 1) ? "+" +  gammalib::fill("=", text.length()-2) + "+"
                             : "+" +  gammalib::fill("-", text.length()-2) + "+";
        break;
    case 3:
        text  = "=== " + arg + " ===";
        break;
    default:
        break;
    }

    // Write header into logger
    if (frame.length() > 0) {
        append(frame);
        append("\n");
    }
    if (text.length() > 0) {
        append(text);
        append("\n");
    }
    if (frame.length() > 0) {
        append(frame);
        append("\n");
    }

    // Return
    return;
}


/***********************************************************************//**
 * @brief Return current date
 *
 * Returns the current date as string in the format yyyy-mm-ddThh:mm:ss.
 ***************************************************************************/
std::string GLog::strdate(void) const
{
    // Allocate variables
    struct std::tm timeStruct;
    std::time_t    now;
    char           buffer[100];

    // Get time
    now = std::time(NULL);
    #ifdef HAVE_GMTIME_R   
    std::gmtime_r(&now, &timeStruct);
    #else
    std::memcpy(&timeStruct, gmtime(&now), sizeof(struct tm));
    #endif

    // Write message type, time and task name to buffer
    std::sprintf(buffer, "%04d-%02d-%02dT%02d:%02d:%02d",
                         timeStruct.tm_year + 1900,
                         timeStruct.tm_mon + 1,
                         timeStruct.tm_mday,
                         timeStruct.tm_hour,
                         timeStruct.tm_min,
                         timeStruct.tm_sec);

    // Build string from buffer
    std::string date = buffer;

    // Return date
    return date;
}


/***********************************************************************//**
 * @brief Return prefix
 *
 * Returns the prefix for each line.
 ***************************************************************************/
std::string GLog::prefix(void) const
{
    // Initialize prefix to empty string
    std::string prefix = "";

    // Add date if requested
    if (m_use_date){
        prefix.append(strdate());
    }

    // Add name if requested
    if (m_name.length() > 0){
        prefix.append(" "+m_name);
    }

    // If there is a prefix then add separator
    if (prefix.length() > 0){
        prefix.append(": ");
    }

    // Add any indent
    prefix.append(gammalib::fill(" ", m_indent));

    // Return prefix
    return prefix;
}


/***********************************************************************//**
 * @brief Append string to the buffer
 *
 * @param[in] arg String to append
 *
 * This method appends a string to the buffer and prepends, if required, the
 * current date and name at the beginning of each line.
 ***************************************************************************/
void GLog::append(std::string arg)
{
    // If the buffer is empty and at the beginning of a line or if the last
    // charater is a \n, prepend a prefix at the beginning of the string to
    // be inserted.
    if ((m_buffer.size() == 0 && m_linestart) ||
        m_buffer[m_buffer.size()-1] == '\n') {

        // Prepend prefix
        arg.insert(0, prefix());

    }

    // Search the first CR (\n)
    std::size_t pos = arg.find_first_of("\n",0);

    // Search all \n characters. Ignore the last CR.
    while (pos != std::string::npos && pos < arg.size()-1) {

        // Prepend prefix
        std::string pre = prefix();
        arg.insert(pos+1, pre);

        // Search next CR
        pos = arg.find_first_of("\n",pos+1+pre.size());

    } // endwhile

    // Add string to buffer
    m_buffer.append(arg);

    // Flush Buffer
    flush();

    // Append string to stdout and/or stderr without any buffering
    // if requested
    if (m_stdout) {
        std::cout << arg;
    }
    if (m_stderr) {
        std::cerr << arg;
    }

    // Return
    return;
}


/*==========================================================================
 =                                                                         =
 =                                 Friends                                 =
 =                                                                         =
 ==========================================================================*/