Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      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 
     17 #include "util/base/logging.h"
     18 
     19 #include <stdlib.h>
     20 
     21 #include <iostream>
     22 
     23 #include "util/base/logging_raw.h"
     24 
     25 namespace libtextclassifier2 {
     26 namespace logging {
     27 
     28 namespace {
     29 // Returns pointer to beginning of last /-separated token from file_name.
     30 // file_name should be a pointer to a zero-terminated array of chars.
     31 // E.g., "foo/bar.cc" -> "bar.cc", "foo/" -> "", "foo" -> "foo".
     32 const char *JumpToBasename(const char *file_name) {
     33   if (file_name == nullptr) {
     34     return nullptr;
     35   }
     36 
     37   // Points to the beginning of the last encountered token.
     38   const char *last_token_start = file_name;
     39   while (*file_name != '\0') {
     40     if (*file_name == '/') {
     41       // Found token separator.  A new (potentially empty) token starts after
     42       // this position.  Notice that if file_name is a valid zero-terminated
     43       // string, file_name + 1 is a valid pointer (there is at least one char
     44       // after address file_name, the zero terminator).
     45       last_token_start = file_name + 1;
     46     }
     47     file_name++;
     48   }
     49   return last_token_start;
     50 }
     51 }  // namespace
     52 
     53 LogMessage::LogMessage(LogSeverity severity, const char *file_name,
     54                        int line_number)
     55     : severity_(severity) {
     56   stream_ << JumpToBasename(file_name) << ":" << line_number << ": ";
     57 }
     58 
     59 LogMessage::~LogMessage() {
     60   LowLevelLogging(severity_, /* tag = */ "txtClsf", stream_.message);
     61   if (severity_ == FATAL) {
     62     exit(1);
     63   }
     64 }
     65 
     66 }  // namespace logging
     67 }  // namespace libtextclassifier2
     68