output.cc revision 2632:1bb2f91485ea
11689SN/A/* 210715SRekai.GonzalezAlberquilla@arm.com * Copyright (c) 2005 The Regents of The University of Michigan 310715SRekai.GonzalezAlberquilla@arm.com * All rights reserved. 410715SRekai.GonzalezAlberquilla@arm.com * 510715SRekai.GonzalezAlberquilla@arm.com * Redistribution and use in source and binary forms, with or without 610715SRekai.GonzalezAlberquilla@arm.com * modification, are permitted provided that the following conditions are 710715SRekai.GonzalezAlberquilla@arm.com * met: redistributions of source code must retain the above copyright 810715SRekai.GonzalezAlberquilla@arm.com * notice, this list of conditions and the following disclaimer; 910715SRekai.GonzalezAlberquilla@arm.com * redistributions in binary form must reproduce the above copyright 1010715SRekai.GonzalezAlberquilla@arm.com * notice, this list of conditions and the following disclaimer in the 1110715SRekai.GonzalezAlberquilla@arm.com * documentation and/or other materials provided with the distribution; 1210715SRekai.GonzalezAlberquilla@arm.com * neither the name of the copyright holders nor the names of its 1310715SRekai.GonzalezAlberquilla@arm.com * contributors may be used to endorse or promote products derived from 141689SN/A * this software without specific prior written permission. 159919Ssteve.reinhardt@amd.com * 161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 271689SN/A */ 281689SN/A 291689SN/A#include <errno.h> 301689SN/A#include <limits.h> 311689SN/A#include <stdlib.h> 321689SN/A#include <sys/stat.h> 331689SN/A#include <sys/types.h> 341689SN/A 351689SN/A#include <fstream> 361689SN/A 371689SN/A#include "base/misc.hh" 381689SN/A#include "base/output.hh" 391689SN/A 402665Ssaidi@eecs.umich.eduusing namespace std; 412665Ssaidi@eecs.umich.edu 429919Ssteve.reinhardt@amd.comOutputDirectory simout; 431689SN/A 441689SN/A/** 451060SN/A * 461061SN/A */ 471060SN/AOutputDirectory::OutputDirectory() 481060SN/A{} 492292SN/A 502292SN/AOutputDirectory::~OutputDirectory() 511060SN/A{} 521060SN/A 531461SN/Avoid 541060SN/AOutputDirectory::setDirectory(const string &d) 551060SN/A{ 566658Snate@binkert.org if (!dir.empty()) 576658Snate@binkert.org panic("Output directory already set!\n"); 581717SN/A 599919Ssteve.reinhardt@amd.com dir = d; 609919Ssteve.reinhardt@amd.com 611060SN/A if (dir != ".") { 629919Ssteve.reinhardt@amd.com if (mkdir(dir.c_str(), 0777) < 0 && errno != EEXIST) 639919Ssteve.reinhardt@amd.com panic("couldn't make output dir %s: %s\n", 649919Ssteve.reinhardt@amd.com dir, strerror(errno)); 659919Ssteve.reinhardt@amd.com } 669919Ssteve.reinhardt@amd.com 679919Ssteve.reinhardt@amd.com // guarantee that directory ends with a '/' 689919Ssteve.reinhardt@amd.com if (dir[dir.size() - 1] != '/') 691060SN/A dir += "/"; 701060SN/A} 719919Ssteve.reinhardt@amd.com 729919Ssteve.reinhardt@amd.comconst string & 739919Ssteve.reinhardt@amd.comOutputDirectory::directory() 749919Ssteve.reinhardt@amd.com{ 759919Ssteve.reinhardt@amd.com if (dir.empty()) 769919Ssteve.reinhardt@amd.com panic("Output directory not set!"); 779919Ssteve.reinhardt@amd.com 789919Ssteve.reinhardt@amd.com return dir; 799919Ssteve.reinhardt@amd.com} 809919Ssteve.reinhardt@amd.com 819919Ssteve.reinhardt@amd.comstring 829919Ssteve.reinhardt@amd.comOutputDirectory::resolve(const string &name) 839919Ssteve.reinhardt@amd.com{ 849919Ssteve.reinhardt@amd.com return (name[0] != '/') ? dir + name : name; 859919Ssteve.reinhardt@amd.com} 869919Ssteve.reinhardt@amd.com 879919Ssteve.reinhardt@amd.comostream * 889919Ssteve.reinhardt@amd.comOutputDirectory::create(const string &name) 899919Ssteve.reinhardt@amd.com{ 909919Ssteve.reinhardt@amd.com if (name == "cerr" || name == "stderr") 911060SN/A return &cerr; 929919Ssteve.reinhardt@amd.com 939919Ssteve.reinhardt@amd.com if (name == "cout" || name == "stdout") 949919Ssteve.reinhardt@amd.com return &cout; 959919Ssteve.reinhardt@amd.com 969919Ssteve.reinhardt@amd.com ofstream *file = new ofstream(resolve(name).c_str(), ios::trunc); 971060SN/A if (!file->is_open()) 989919Ssteve.reinhardt@amd.com panic("Cannot open file %s", name); 999919Ssteve.reinhardt@amd.com 1009919Ssteve.reinhardt@amd.com return file; 1011060SN/A} 1029919Ssteve.reinhardt@amd.com 1031060SN/Aostream * 1041060SN/AOutputDirectory::find(const string &name) 1051060SN/A{ 1061060SN/A if (name == "cerr" || name == "stderr") 1071060SN/A return &cerr; 1081060SN/A 1091060SN/A if (name == "cout" || name == "stdout") 1101461SN/A return &cout; 1111060SN/A 1129919Ssteve.reinhardt@amd.com string filename = resolve(name); 1139919Ssteve.reinhardt@amd.com map_t::iterator i = files.find(filename); 1149919Ssteve.reinhardt@amd.com if (i != files.end()) 1159919Ssteve.reinhardt@amd.com return (*i).second; 1169919Ssteve.reinhardt@amd.com 1179919Ssteve.reinhardt@amd.com ofstream *file = new ofstream(filename.c_str(), ios::trunc); 1189919Ssteve.reinhardt@amd.com if (!file->is_open()) 1199919Ssteve.reinhardt@amd.com panic("Cannot open file %s", filename); 1209919Ssteve.reinhardt@amd.com 1219919Ssteve.reinhardt@amd.com files[filename] = file; 1229919Ssteve.reinhardt@amd.com return file; 1239919Ssteve.reinhardt@amd.com} 1249919Ssteve.reinhardt@amd.com 1259919Ssteve.reinhardt@amd.combool 1269919Ssteve.reinhardt@amd.comOutputDirectory::isFile(const std::ostream *os) 1279919Ssteve.reinhardt@amd.com{ 1289919Ssteve.reinhardt@amd.com return os && os != &cerr && os != &cout; 1299919Ssteve.reinhardt@amd.com} 1309919Ssteve.reinhardt@amd.com