reporter.cpp

00001 /*
00002  *  TOPPERS/FDMP Kernel
00003  *      Toyohashi Open Platform for Embedded Real-Time Systems/
00004  *      Function Distributed Multiprocessor Kernel
00005  *
00006  *  Copyright (C) 2004 by Witz Corporation, JAPAN  
00007  *  Copyright (C) 2005 by Takagi Nobuhisa
00008  * 
00009  *  上記著作権者は,以下の (1)〜(4) の条件か,Free Software Foundation 
00010  *  によって公表されている GNU General Public License の Version 2 に記
00011  *  述されている条件を満たす場合に限り,本ソフトウェア(本ソフトウェア
00012  *  を改変したものを含む.以下同じ)を使用・複製・改変・再配布(以下,
00013  *  利用と呼ぶ)することを無償で許諾する.
00014  *  (1) 本ソフトウェアをソースコードの形で利用する場合には,上記の著作
00015  *      権表示,この利用条件および下記の無保証規定が,そのままの形でソー
00016  *      スコード中に含まれていること.
00017  *  (2) 本ソフトウェアを,ライブラリ形式など,他のソフトウェア開発に使
00018  *      用できる形で再配布する場合には,再配布に伴うドキュメント(利用
00019  *      者マニュアルなど)に,上記の著作権表示,この利用条件および下記
00020  *      の無保証規定を掲載すること.
00021  *  (3) 本ソフトウェアを,機器に組み込むなど,他のソフトウェア開発に使
00022  *      用できない形で再配布する場合には,次のいずれかの条件を満たすこ
00023  *      と.
00024  *    (a) 再配布に伴うドキュメント(利用者マニュアルなど)に,上記の著
00025  *        作権表示,この利用条件および下記の無保証規定を掲載すること.
00026  *    (b) 再配布の形態を,別に定める方法によって,TOPPERSプロジェクトに
00027  *        報告すること.
00028  *  (4) 本ソフトウェアの利用により直接的または間接的に生じるいかなる損
00029  *      害からも,上記著作権者およびTOPPERSプロジェクトを免責すること.
00030  * 
00031  *  本ソフトウェアは,無保証で提供されているものである.上記著作権者お
00032  *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,その適用可能性も
00033  *  含めて,いかなる保証も行わない.また,本ソフトウェアの利用により直
00034  *  接的または間接的に生じたいかなる損害に関しても,その責任を負わない.
00035  * 
00036  */
00037 
00038 /*
00039  *  toppers/reporter.cpp
00040  */
00041 #include "toppers/reporter.hpp"
00042 #include <iostream>
00043 
00044 namespace toppers
00045 {
00046 
00047   namespace
00048   {
00049     const char* program_name = "cfg";
00050   }
00051 
00058   std::ostream* reporter::default_stream_ = &std::cerr;
00059 
00066   reporter::translator_type reporter::translator_ = 0;
00067 
00074   int reporter::error_count_ = 1;
00075 
00080   reporter::reporter( std::ostream* stream ) : stream_( stream )
00081   {
00082     if ( stream_ == 0 )
00083     { 
00084       stream_ = default_stream_;
00085     }
00086   }
00087 
00091   reporter::~reporter()
00092   { 
00093     stream_ = 0;
00094   }
00095 
00100   void reporter::warning( const boost::format& fmt ) const
00101   { 
00102     *stream_ << program_name << ": " << _( "warning: " ) << fmt << std::endl; 
00103   }
00104 
00111   void reporter::warning( const std::string& file, int line, const boost::format& fmt ) const
00112   { 
00113     *stream_ << program_name << ": " << file << ':' << line << ": " << _( "warning: " ) << fmt << std::endl; 
00114   }
00115 
00121   void reporter::error( const boost::format& fmt ) const
00122   {
00123     *stream_ << program_name << ": " << _( "error: " ) << fmt << std::endl;
00124     if ( --error_count_ <= 0 )
00125     {
00126       abort();
00127     }
00128   }
00129 
00137   void reporter::error( const std::string& file, int line, const boost::format& fmt ) const
00138   {
00139     *stream_ << program_name << ": " << file << ':' << line << ": " << _( "error: " ) << fmt << std::endl;
00140     if ( --error_count_ <= 0 )
00141     {
00142       abort();
00143     }
00144   }
00145 
00151   void reporter::fatal( const boost::format& fmt ) const
00152   {
00153     *stream_ << program_name << ": " << _( "fatal error: " ) << fmt << std::endl;
00154     abort();
00155   }
00156 
00164   void reporter::fatal( const std::string& file, int line, const boost::format& fmt ) const
00165   {
00166     *stream_ << program_name << ": " << file << ':' << line << ": " << _( "fatal error: " ) << fmt << std::endl;
00167     abort();
00168   }
00169 
00175   void reporter::stop( const boost::format& fmt ) const
00176   {
00177     *stream_ << program_name << ": " << fmt << std::endl;
00178     abort();
00179   }
00180 
00185   void reporter::set_program_name( const char* name )
00186   {
00187     program_name = name;
00188   }
00189 
00194   void reporter::set_default_stream( std::ostream* ostr )
00195   { 
00196     default_stream_ = ostr; 
00197   }
00198 
00207   const char* reporter::translate( const char* str )
00208   {
00209     if ( translator_ != 0 )
00210     { 
00211       return ( *translator_ ) ( str ); 
00212     }
00213     return str;
00214   }
00215 
00222   reporter::translator_type reporter::set_translator( translator_type func )
00223   {
00224     translator_type prev = translator_;
00225     translator_ = func;
00226     return prev;
00227   }
00228 
00239   void reporter::abort() const
00240   { 
00241     throw terminator(); 
00242   }
00243 
00244   void warning( const text_line& line, const boost::format& fmt )
00245   {
00246     reporter r;
00247     if ( line.line_ > 0 )
00248     {
00249       r.warning( line.file_.native_file_string(), line.line_, fmt );
00250     }
00251     else
00252     {
00253       r.warning( fmt );
00254     }
00255   }
00256 
00257   void error( const text_line& line, const boost::format& fmt )
00258   {
00259     reporter r;
00260     if ( line.line_ > 0 )
00261     {
00262       r.error( line.file_.native_file_string(), line.line_, fmt );
00263     }
00264     else
00265     {
00266       r.error( fmt );
00267     }
00268   }
00269 
00270   void fatal( const text_line& line, const boost::format& fmt )
00271   {
00272     reporter r;
00273     if ( line.line_ > 0 )
00274     {
00275       r.fatal( line.file_.native_file_string(), line.line_, fmt );
00276     }
00277     else
00278     {
00279       r.fatal( fmt );
00280     }
00281   }
00282 
00283 }

Copyright © 2006 by TAKAGI Nobuhisa.
このページは Wed Apr 12 16:31:57 2006 に Doxygen によって生成されました。