URI: 
       tLogger.cc - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
  HTML git clone git://src.adamsgaard.dk/pism
   DIR Log
   DIR Files
   DIR Refs
   DIR LICENSE
       ---
       tLogger.cc (3378B)
       ---
            1 /* Copyright (C) 2015, 2017 PISM Authors
            2  *
            3  * This file is part of PISM.
            4  *
            5  * PISM is free software; you can redistribute it and/or modify it under the
            6  * terms of the GNU General Public License as published by the Free Software
            7  * Foundation; either version 3 of the License, or (at your option) any later
            8  * version.
            9  *
           10  * PISM is distributed in the hope that it will be useful, but WITHOUT ANY
           11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
           12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
           13  * details.
           14  *
           15  * You should have received a copy of the GNU General Public License
           16  * along with PISM; if not, write to the Free Software
           17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
           18  */
           19 
           20 #include <unistd.h>
           21 #include <sstream>
           22 #include <stdarg.h>
           23 #include <petscsys.h>
           24 
           25 #include "Logger.hh"
           26 #include "pism_utilities.hh"
           27 #include "pism_options.hh"
           28 #include "error_handling.hh"
           29 
           30 namespace pism {
           31 
           32 struct Logger::Impl {
           33   MPI_Comm com;
           34   bool enabled;
           35   int threshold;
           36 };
           37 
           38 Logger::Logger(MPI_Comm com, int threshold)
           39   : m_impl(new Impl) {
           40 
           41   m_impl->com = com;
           42   m_impl->enabled = true;
           43   m_impl->threshold = threshold;
           44 }
           45 
           46 Logger::~Logger() {
           47   delete m_impl;
           48 }
           49 
           50 void Logger::message(int threshold, const char format[], ...) const {
           51   if ((not m_impl->enabled) or threshold > m_impl->threshold) {
           52     return;
           53   }
           54 
           55   char buffer[8192];
           56   va_list argp;
           57 
           58   va_start(argp, format);
           59   vsnprintf(buffer, sizeof(buffer), format, argp);
           60   va_end(argp);
           61 
           62   message_impl(buffer);
           63 }
           64 
           65 void Logger::message(int threshold, const std::string &buffer) const {
           66   if ((not m_impl->enabled) or threshold > m_impl->threshold) {
           67     return;
           68   }
           69 
           70   message_impl(buffer.c_str());
           71 }
           72 
           73 void Logger::message_impl(const char buffer[]) const {
           74   PetscErrorCode ierr = PetscFPrintf(m_impl->com, PETSC_STDOUT, "%s", buffer);
           75   PISM_CHK(ierr, "PetscFPrintf");
           76 }
           77 
           78 void Logger::error(const char format[], ...) const {
           79   char buffer[8192];
           80   va_list argp;
           81 
           82   va_start(argp, format);
           83   vsnprintf(buffer, sizeof(buffer), format, argp);
           84   va_end(argp);
           85 
           86   error_impl(buffer);
           87 }
           88 
           89 void Logger::error_impl(const char buffer[]) const {
           90   PetscErrorCode ierr = PetscFPrintf(m_impl->com, stderr, buffer);
           91   PISM_CHK(ierr, "PetscFPrintf");
           92 }
           93 
           94 void Logger::set_threshold(int level) {
           95   m_impl->threshold = level;
           96 }
           97 int Logger::get_threshold() const {
           98   return m_impl->threshold;
           99 }
          100 void Logger::disable() const {
          101   m_impl->enabled = false;
          102 }
          103 
          104 void Logger::enable() const {
          105   m_impl->enabled = true;
          106 }
          107 
          108 Logger::Ptr logger_from_options(MPI_Comm com) {
          109   Logger::Ptr result(new Logger(com, 2));
          110 
          111   options::Integer verbosity("-verbose", "set logger verbosity threshold",
          112                              result->get_threshold());
          113 
          114   result->set_threshold(verbosity);
          115 
          116   return result;
          117 }
          118 
          119 struct StringLogger::Impl {
          120   std::ostringstream data;
          121 };
          122 
          123 StringLogger::StringLogger(MPI_Comm com, int threshold)
          124   : Logger(com, threshold), m_impl(new Impl) {
          125   // empty
          126 }
          127 
          128 StringLogger::~StringLogger() {
          129   delete m_impl;
          130 }
          131 
          132 void StringLogger::message_impl(const char buffer[]) const {
          133   m_impl->data << buffer;
          134 }
          135 
          136 void StringLogger::error_impl(const char buffer[]) const {
          137   m_impl->data << buffer;
          138 }
          139 
          140 std::string StringLogger::get() const {
          141   return m_impl->data.str();
          142 }
          143 
          144 void StringLogger::reset() {
          145   m_impl->data.str("");
          146 }
          147 
          148 
          149 } // end of namespace pism