Home | History | Annotate | Download | only in src
      1 // Copyright 2008 Google Inc.
      2 // Author: Lincoln Smith
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 
     16 #ifndef OPEN_VCDIFF_LOGGING_H_
     17 #define OPEN_VCDIFF_LOGGING_H_
     18 
     19 #include <config.h>
     20 #include <iostream>
     21 #include <vector>
     22 
     23 // Windows API defines ERROR
     24 #ifdef ERROR
     25 #undef ERROR
     26 #endif  // ERROR
     27 
     28 namespace open_vcdiff {
     29 
     30 enum LogLevel {
     31   INFO,
     32   WARNING,
     33   ERROR,
     34   FATAL
     35 };
     36 
     37 #ifndef NDEBUG
     38 #define DFATAL FATAL
     39 #else  // NDEBUG
     40 #define DFATAL ERROR
     41 #endif  // !NDEBUG
     42 
     43 extern bool g_fatal_error_occurred;
     44 extern void (*ExitFatal)();
     45 
     46 inline std::ostream& LogMessage(LogLevel level, const char* level_name) {
     47   if (level == FATAL) {
     48     g_fatal_error_occurred = true;
     49   }
     50   return std::cerr << level_name << ": ";
     51 }
     52 
     53 inline void CheckFatalError() {
     54   if (g_fatal_error_occurred) {
     55     g_fatal_error_occurred = false;
     56     (*ExitFatal)();
     57   }
     58 }
     59 
     60 }  // namespace open_vcdiff
     61 
     62 #define LOG(level)   LogMessage(open_vcdiff::level, #level)
     63 #define LOG_ENDL     std::endl; \
     64                      open_vcdiff::CheckFatalError();
     65 
     66 #endif  // OPEN_VCDIFF_LOGGING_H_
     67