23 #include <boost/filesystem.hpp> 24 #include <boost/format.hpp> 26 #include "application_base.h" 27 #include "core_helpers.h" 28 #include "goby/common/configuration_reader.h" 36 int goby::common::ApplicationBase::argc_ = 0;
37 char** goby::common::ApplicationBase::argv_ = 0;
40 : all_cfg_(cfg), base_cfg_(0), own_base_cfg_(false), alive_(true)
45 boost::program_options::options_description od(
"Allowed options");
46 boost::program_options::variables_map var_map;
50 common::ConfigReader::read_cfg(argc_, argv_, cfg, &application_name, &od, &var_map);
56 const google::protobuf::Descriptor* desc = cfg->GetDescriptor();
57 for (
int i = 0, n = desc->field_count(); i < n; ++i)
59 const google::protobuf::FieldDescriptor* field_desc = desc->field(i);
60 if (field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE &&
61 field_desc->message_type() == ::AppBaseConfig::descriptor())
64 cfg->GetReflection()->MutableMessage(cfg, field_desc));
75 base_cfg_->set_app_name(application_name);
78 merge_app_base_cfg(base_cfg_, var_map);
80 catch (common::ConfigException& e)
85 std::cerr << od <<
"\n";
86 std::cerr <<
"Problem parsing command-line configuration: \n" << e.what() <<
"\n";
94 static_cast<common::logger::Verbosity>(base_cfg_->glog_config().tty_verbosity()),
97 if (base_cfg_->glog_config().show_gui())
100 fout_.resize(base_cfg_->glog_config().file_log_size());
101 for (
int i = 0, n = base_cfg_->glog_config().file_log_size(); i < n; ++i)
105 boost::format file_format(base_cfg_->glog_config().file_log(i).file_name());
106 file_format.exceptions(boost::io::all_error_bits ^
107 (boost::io::too_many_args_bit | boost::io::too_few_args_bit));
109 std::string file_name = (file_format % to_iso_string(second_clock::universal_time())).str();
110 std::string file_symlink = (file_format %
"latest").str();
112 glog.is(VERBOSE) &&
glog <<
"logging output to file: " << file_name << std::endl;
114 fout_[i].reset(
new std::ofstream(file_name.c_str()));
116 if (!fout_[i]->is_open())
118 <<
"cannot write glog output to requested file: " << file_name
121 remove(file_symlink.c_str());
122 namespace fs = boost::filesystem;
123 fs::path canonicalized_file_name = fs::canonical(fs::path(file_name.c_str()));
124 symlink(canonicalized_file_name.string().c_str(), file_symlink.c_str());
126 glog.
add_stream(base_cfg_->glog_config().file_log(i).verbosity(), fout_[i].get());
129 if (!base_cfg_->IsInitialized())
130 throw(common::ConfigException(
"Invalid base configuration"));
135 goby::common::ApplicationBase::~ApplicationBase()
137 glog.is(DEBUG1) &&
glog <<
"ApplicationBase destructing..." << std::endl;
143 void goby::common::ApplicationBase::__run()
146 sigset_t signal_mask;
147 sigemptyset(&signal_mask);
148 sigaddset(&signal_mask, SIGWINCH);
149 pthread_sigmask(SIG_BLOCK, &signal_mask, NULL);
void set_name(const std::string &s)
Set the name of the application that the logger is serving.
std::string application_name()
name of this application (from AppBaseConfig::app_name). E.g. "garmin_gps_g"
common::FlexOstream glog
Access the Goby logger through this object.
void add_stream(logger::Verbosity verbosity=logger::VERBOSE, std::ostream *os=0)
Attach a stream object (e.g. std::cout, std::ofstream, ...) to the logger with desired verbosity...