logging.cc revision 12376
112376Sgabeblack@google.com/* 212376Sgabeblack@google.com * Copyright 2017 Google Inc. 312376Sgabeblack@google.com * 412376Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 512376Sgabeblack@google.com * modification, are permitted provided that the following conditions are 612376Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 712376Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 812376Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 912376Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1012376Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1112376Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1212376Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1312376Sgabeblack@google.com * this software without specific prior written permission. 1412376Sgabeblack@google.com * 1512376Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1612376Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1712376Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1812376Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1912376Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2012376Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2112376Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2212376Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2312376Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2412376Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2512376Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2612376Sgabeblack@google.com */ 2712376Sgabeblack@google.com 2812376Sgabeblack@google.com#include <gtest/gtest.h> 2912376Sgabeblack@google.com 3012376Sgabeblack@google.com#include <array> 3112376Sgabeblack@google.com#include <string> 3212376Sgabeblack@google.com 3312376Sgabeblack@google.com#include "base/cprintf.hh" 3412376Sgabeblack@google.com#include "base/logging.hh" 3512376Sgabeblack@google.com 3612376Sgabeblack@google.comnamespace { 3712376Sgabeblack@google.com 3812376Sgabeblack@google.com// This custom exception type will help prevent fatal exceptions from being 3912376Sgabeblack@google.com// caught by other code in gem5 and let them escape to the gtest framework. 4012376Sgabeblack@google.com// Unfortunately that results in a somewhat confusing message about an unknown 4112376Sgabeblack@google.com// exception being thrown after the panic/fatal message has been printed, but 4212376Sgabeblack@google.com// there will at least be some indication what went wrong. 4312376Sgabeblack@google.comstruct GTestException 4412376Sgabeblack@google.com{}; 4512376Sgabeblack@google.com 4612376Sgabeblack@google.comclass GTestLogger : public Logger 4712376Sgabeblack@google.com{ 4812376Sgabeblack@google.com public: 4912376Sgabeblack@google.com using Logger::Logger; 5012376Sgabeblack@google.com 5112376Sgabeblack@google.com protected: 5212376Sgabeblack@google.com void log(const Loc &loc, std::string s) override { SUCCEED() << s; } 5312376Sgabeblack@google.com}; 5412376Sgabeblack@google.com 5512376Sgabeblack@google.comclass GTestExitLogger : public Logger 5612376Sgabeblack@google.com{ 5712376Sgabeblack@google.com public: 5812376Sgabeblack@google.com using Logger::Logger; 5912376Sgabeblack@google.com 6012376Sgabeblack@google.com protected: 6112376Sgabeblack@google.com void 6212376Sgabeblack@google.com log(const Loc &loc, std::string s) override 6312376Sgabeblack@google.com { 6412376Sgabeblack@google.com ADD_FAILURE_AT(loc.file, loc.line) << s; 6512376Sgabeblack@google.com } 6612376Sgabeblack@google.com // Throw an exception to escape down to the gtest framework. 6712376Sgabeblack@google.com void exit() override { throw GTestException(); } 6812376Sgabeblack@google.com}; 6912376Sgabeblack@google.com 7012376Sgabeblack@google.comGTestExitLogger panicLogger("panic: "); 7112376Sgabeblack@google.comGTestExitLogger fatalLogger("fatal: "); 7212376Sgabeblack@google.comGTestLogger warnLogger("warn: "); 7312376Sgabeblack@google.comGTestLogger infoLogger("info: "); 7412376Sgabeblack@google.comGTestLogger hackLogger("hack: "); 7512376Sgabeblack@google.com 7612376Sgabeblack@google.com} // anonymous namespace 7712376Sgabeblack@google.com 7812376Sgabeblack@google.comLogger &Logger::getPanic() { return panicLogger; } 7912376Sgabeblack@google.comLogger &Logger::getFatal() { return fatalLogger; } 8012376Sgabeblack@google.comLogger &Logger::getWarn() { return warnLogger; } 8112376Sgabeblack@google.comLogger &Logger::getInfo() { return infoLogger; } 8212376Sgabeblack@google.comLogger &Logger::getHack() { return hackLogger; } 83