libi2ncommon: (reinhard) added method to ask if logging to log file is active. enable...
authorReinhard Pfau <reinhard.pfau@intra2net.com>
Mon, 20 Oct 2008 09:09:34 +0000 (09:09 +0000)
committerReinhard Pfau <reinhard.pfau@intra2net.com>
Mon, 20 Oct 2008 09:09:34 +0000 (09:09 +0000)
src/logfunc.cpp
src/logfunc.hpp

index fff1c03..10f44f7 100644 (file)
@@ -266,6 +266,44 @@ void log_part_msg(
 } // 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()
 {
@@ -536,18 +574,7 @@ void enable_syslog( Facility facility )
 {
     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;
@@ -603,15 +630,21 @@ void enable_log_file( const std::string& name )
 
 /**
  * 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 );
         }
@@ -623,6 +656,15 @@ void enable_log_file( bool enable )
 } // 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).
index a6e0be4..0e61cab 100644 (file)
@@ -184,6 +184,8 @@ void enable_stderr_log(bool enable= true );
 void enable_log_file( const std::string& name );
 void enable_log_file( bool enable= true );
 
+bool is_logging_to_file();
+
 
 void reopen();