/* G-Cows - scripting language for creation of web sites Copyright (C) 2002, 2003, 2004, 2005, 2006 Andrea Sozzi This file is part of G-Cows. G-Cows is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. G-Cows is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifndef LIB_ERROR_H #define LIB_ERROR_H #include #include #include #include "common.H" using std::string; class Log_infos { public: Log_infos () : dir (""), file (""), line (0) { } void erase () { dir = ""; file = ""; line = 0; } string dir; string file; int line; }; typedef std::vector Log_infos_vector; typedef Log_infos_vector::iterator Log_infos_iter; class Msg { public: static Msg *inst () { if (instance == 0) instance = new Msg (); return instance; } enum { parsing_log_mode, exec_log_mode, standard, silent, verbose, exit_on_error, exit_on_warning, never_exit }; void set_program_name (char *name) { program_name = string (name); } void set_brief (bool brief) { brief_flag = brief; } void set_verbosity (int verbosity) { verb = verbosity; } void set_exit_mode (int exit_mode) { exit_on = exit_mode; } void new_flags (bool brief, int verbosity, int exit_mode) { brief_flag = brief; verb = verbosity; exit_on = exit_mode; } void opt (string long_msg, string short_msg = "") { if (verb == verbose) out (long_msg, short_msg, "info"); } void log (string long_msg, string short_msg = "") { if (verb != silent) out (long_msg, short_msg, "info"); } void warning (string long_msg, string short_msg = "") { n_warnings++; if (verb != silent) out (long_msg, short_msg, "warning"); if (exit_on == exit_on_warning) exit (EXIT_FAILURE); } void error (string long_msg, string short_msg = "") { n_errors++; if (verb != silent) out (long_msg, short_msg, "error"); if (exit_on != never_exit) exit (EXIT_FAILURE); } void error (int lines_back, string long_msg, string short_msg = "") { n_errors++; if (verb != silent) out (long_msg, short_msg, "error", lines_back); if (exit_on != never_exit) exit (EXIT_FAILURE); } void fatal (string long_msg, string short_msg = "") { if (verb != silent) out (long_msg, short_msg, "fatal"); exit (EXIT_FAILURE); } void sys_error (string long_msg, string short_msg = "") { error (long_msg + "; " + strerror (errno), short_msg + "; " + strerror (errno)); } void sys_fatal (string long_msg, string short_msg = "") { fatal (long_msg + "; " + strerror (errno), short_msg + "; " + strerror (errno)); } // Functions related to run-time diagnostic void set_log_mode (int mode) { log_mode = mode; } void set_exec_log_infos (Log_infos log_infos) { exec_log_infos = log_infos; } // Functions related to parse-time diagnostic void set_parsed_file (string file); void save_buffer () { parse_log_infos_stack.push_back (parse_log_infos); } void restore_saved_buffer (); void erase_buffer () { parse_log_infos.erase (); } void advance_line () { parse_log_infos.line++; } void advance_line (const char *token); Log_infos get_parse_infos () { return parse_log_infos; } // Functions related to report features int get_n_warnings () { return n_warnings; } int get_n_errors () { return n_errors; } protected: Msg () : brief_flag (false), verb (0), exit_on (0), log_mode (parsing_log_mode), n_warnings (0), n_errors (0) {} private: static Msg *instance; string program_name; bool brief_flag; int verb; int exit_on; int log_mode; Log_infos parse_log_infos; Log_infos_vector parse_log_infos_stack; Log_infos exec_log_infos; int n_warnings; int n_errors; //void out (string long_msg, string short_msg, const char *type = NULL); void out (string long_msg, string short_msg, const char *type, int lines_back=0); }; #endif // ! LIB_ERROR_H