logging.cc revision 12334
112334Sgabeblack@google.com/* 212334Sgabeblack@google.com * Copyright (c) 2014, 2017 ARM Limited 312334Sgabeblack@google.com * All rights reserved 412334Sgabeblack@google.com * 512334Sgabeblack@google.com * The license below extends only to copyright in the software and shall 612334Sgabeblack@google.com * not be construed as granting a license to any other intellectual 712334Sgabeblack@google.com * property including but not limited to intellectual property relating 812334Sgabeblack@google.com * to a hardware implementation of the functionality of the software 912334Sgabeblack@google.com * licensed hereunder. You may use the software subject to the license 1012334Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated 1112334Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software, 1212334Sgabeblack@google.com * modified or unmodified, in source code or in binary form. 1312334Sgabeblack@google.com * 1412334Sgabeblack@google.com * Copyright (c) 2002-2005 The Regents of The University of Michigan 1512334Sgabeblack@google.com * All rights reserved. 1612334Sgabeblack@google.com * 1712334Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 1812334Sgabeblack@google.com * modification, are permitted provided that the following conditions are 1912334Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 2012334Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 2112334Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 2212334Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 2312334Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 2412334Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 2512334Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 2612334Sgabeblack@google.com * this software without specific prior written permission. 2712334Sgabeblack@google.com * 2812334Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2912334Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 3012334Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 3112334Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 3212334Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 3312334Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 3412334Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 3512334Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 3612334Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 3712334Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 3812334Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 3912334Sgabeblack@google.com * 4012334Sgabeblack@google.com * Authors: Nathan Binkert 4112334Sgabeblack@google.com * Andreas Sandberg 4212334Sgabeblack@google.com */ 4312334Sgabeblack@google.com 4412334Sgabeblack@google.com#include "base/logging.hh" 4512334Sgabeblack@google.com 4612334Sgabeblack@google.com#include <array> 4712334Sgabeblack@google.com 4812334Sgabeblack@google.com#include "base/hostinfo.hh" 4912334Sgabeblack@google.com#include "base/output.hh" 5012334Sgabeblack@google.com#include "base/trace.hh" 5112334Sgabeblack@google.com#include "base/types.hh" 5212334Sgabeblack@google.com#include "sim/core.hh" 5312334Sgabeblack@google.com 5412334Sgabeblack@google.comvoid 5512334Sgabeblack@google.comLogger::setLevel(LogLevel ll) 5612334Sgabeblack@google.com{ 5712334Sgabeblack@google.com for (int i = 0; i < NUM_LOG_LEVELS; ++i) 5812334Sgabeblack@google.com get(LogLevel(i)).enabled = (i <= ll); 5912334Sgabeblack@google.com} 6012334Sgabeblack@google.com 6112334Sgabeblack@google.comstatic void 6212334Sgabeblack@google.comnewline_if_needed(std::ostream &stream, const char *format) 6312334Sgabeblack@google.com{ 6412334Sgabeblack@google.com const size_t format_len(strlen(format)); 6512334Sgabeblack@google.com 6612334Sgabeblack@google.com switch (format_len ? format[format_len - 1] : '\0') { 6712334Sgabeblack@google.com case '\n': 6812334Sgabeblack@google.com case '\r': 6912334Sgabeblack@google.com break; 7012334Sgabeblack@google.com default: 7112334Sgabeblack@google.com stream << std::endl; 7212334Sgabeblack@google.com } 7312334Sgabeblack@google.com} 7412334Sgabeblack@google.com 7512334Sgabeblack@google.comLogger::Logger(std::ostream &_stream, const char *_prefix) 7612334Sgabeblack@google.com : enabled(true), verbose(false), stream(_stream), prefix(_prefix) 7712334Sgabeblack@google.com{ 7812334Sgabeblack@google.com} 7912334Sgabeblack@google.com 8012334Sgabeblack@google.comvoid 8112334Sgabeblack@google.comLogger::printEpilogue(const char *func, const char *file, int line, 8212334Sgabeblack@google.com const char *format) 8312334Sgabeblack@google.com{ 8412334Sgabeblack@google.com newline_if_needed(stream, format); 8512334Sgabeblack@google.com 8612334Sgabeblack@google.com if (verbose) { 8712334Sgabeblack@google.com ccprintf(stream, " @ tick %d\n[%s:%s, line %d]\n", 8812334Sgabeblack@google.com curTick(), func, file, line); 8912334Sgabeblack@google.com } 9012334Sgabeblack@google.com} 9112334Sgabeblack@google.com 9212334Sgabeblack@google.comclass ExitLogger : public Logger 9312334Sgabeblack@google.com{ 9412334Sgabeblack@google.com public: 9512334Sgabeblack@google.com using Logger::Logger; 9612334Sgabeblack@google.com 9712334Sgabeblack@google.com void printEpilogue(const char *func, const char *file, int line, 9812334Sgabeblack@google.com const char *format) override; 9912334Sgabeblack@google.com}; 10012334Sgabeblack@google.com 10112334Sgabeblack@google.comvoid 10212334Sgabeblack@google.comExitLogger::printEpilogue(const char *func, const char *file, int line, 10312334Sgabeblack@google.com const char *format) 10412334Sgabeblack@google.com{ 10512334Sgabeblack@google.com Logger::printEpilogue(func, file, line, format); 10612334Sgabeblack@google.com 10712334Sgabeblack@google.com ccprintf(stream, "Memory Usage: %ld KBytes\n", memUsage()); 10812334Sgabeblack@google.com} 10912334Sgabeblack@google.com 11012334Sgabeblack@google.comLogger & 11112334Sgabeblack@google.comLogger::get(LogLevel ll) 11212334Sgabeblack@google.com{ 11312334Sgabeblack@google.com static std::array<Logger *, NUM_LOG_LEVELS> loggers{{ 11412334Sgabeblack@google.com new ExitLogger(std::cerr, "panic"), 11512334Sgabeblack@google.com new ExitLogger(std::cerr, "fatal"), 11612334Sgabeblack@google.com new Logger(std::cerr, "warn"), 11712334Sgabeblack@google.com new Logger(std::cerr, "info"), 11812334Sgabeblack@google.com new Logger(std::cerr, "hack"), 11912334Sgabeblack@google.com }}; 12012334Sgabeblack@google.com 12112334Sgabeblack@google.com return *loggers[ll]; 12212334Sgabeblack@google.com} 123