} // eo log_part_msg(int,const std::string&,const std::string&)
+/**
+ * @brief returns the name of the program (/binary)
+ * @return the program name if it could be determined; generated name else.
+ *
+ * Tries to determine the name of the binary.
+ *
+ * If no name could be determined, one is built.
+ */
+std::string get_program_name()
+{
+ std::string result;
+ // determine the program name:
+ {
+ // try to determine the name using the exe link:
+ std::string exe_path;
+ {
+ std::ostringstream ostr;
+ ostr << "/proc/" << ::getpid() << "/exe";
+ exe_path= ostr.str();
+ }
+ std::string binary_path= read_link(exe_path);
+ if (!binary_path.empty())
+ {
+ result= basename(binary_path);
+ }
+ }
+ if (result.empty())
+ {
+ // no program name found up to this point.
+ // make a name (as fallback solution):
+ std::ostringstream ostr;
+ ostr << "prg-" << ::getpid();
+ result= ostr.str();
+ }
+ return result;
+} // eo get_program_name
+
+
void _cleanup()
{
{
if (g_ident.empty())
{
- // determine the program name:
- std::string exe_path;
- {
- std::ostringstream ostr;
- ostr << "/proc/" << ::getpid() << "/exe";
- exe_path= ostr.str();
- }
- std::string binary_path= read_link(exe_path);
- if (!binary_path.empty())
- {
- g_ident= basename(binary_path);
- }
+ g_ident= get_program_name();
}
close_syslog();
g_facility = facility;
/**
* enable or disable loggin to a file.
- * enabling required a filename to be set by a previous call to
- * enable_log_file(const std::string&).
+ * if a logfile was already set by a previous call to enable_log_file(const std::string&)
+ * that one is used; else it logs to <tt>/var/log/</tt><em>program name</em><tt>.log</tt>.
* @param enable whether to enable or disable logging to a file.
*/
void enable_log_file( bool enable )
{
if (enable)
{
- if (! g_log_file_name.empty())
+ if (g_log_file_name.empty())
+ {
+ std::ostringstream ostr;
+ ostr << "/var/log/" << get_program_name() << ".log";
+ enable_log_file( ostr.str() );
+ }
+ else
{
enable_log_file( g_log_file_name );
}
} // eo enable_log_file(bool)
+/**
+ * @brief returns if loging to file is enabled and active.
+ * @return @a true if logfile is enabled and opened.
+ */
+bool is_logging_to_file()
+{
+ return g_log_stream_ptr and g_log_stream_ptr->good();
+} // eo is_logging_to_file()
+
/**
* @brief re-opens the logfiles (if applicable).