]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/message.c
Strip pathname portion off all message routines that print filename:line.
[bacula/bacula] / bacula / src / lib / message.c
index b5d5990b05cb3a2616d4ef26e7cdb9900f85f52b..44070a79e5837486f0853d2b666b891ffe0ec6f4 100755 (executable)
@@ -7,7 +7,7 @@
  *
  */
 /*
-   Copyright (C) 2000-2005 Kern Sibbald
+   Copyright (C) 2000-2006 Kern Sibbald
 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
 #include "bacula.h"
 #include "jcr.h"
 
-#if !defined(HAVE_CONSOLE)
-#if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
-#include <windows.h>
-#endif
-#endif
+sql_query p_sql_query = NULL;
 
 #define FULL_LOCATION 1               /* set for file:line in Debug messages */
 
@@ -51,25 +47,22 @@ char con_fname[500];                  /* Console filename */
 FILE *con_fd = NULL;                  /* Console file descriptor */
 brwlock_t con_lock;                   /* Console lock structure */
 
-#ifdef HAVE_POSTGRESQL
+
+#if defined(HAVE_POSTGRESQL)
 char catalog_db[] = "PostgreSQL";
-#else
-#ifdef HAVE_MYSQL
+#elif defined(HAVE_MYSQL)
 char catalog_db[] = "MySQL";
-#else
-#ifdef HAVE_SQLITE
+#elif defined(HAVE_SQLITE)
 char catalog_db[] = "SQLite";
 #else
 char catalog_db[] = "Internal";
 #endif
-#endif
-#endif
 
 const char *host_os = HOST_OS;
 const char *distname = DISTNAME;
 const char *distver = DISTVER;
 static FILE *trace_fd = NULL;
-#ifdef HAVE_WIN32
+#if defined(HAVE_WIN32)
 static bool trace = true;
 #else
 static bool trace = false;
@@ -116,7 +109,7 @@ void my_name_is(int argc, char *argv[], const char *name)
          l++;
       } else {
          l = argv[0];
-#if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
+#if defined(HAVE_WIN32)
          /* On Windows allow c: junk */
          if (l[1] == ':') {
             l += 2;
@@ -161,13 +154,12 @@ void
 init_msg(JCR *jcr, MSGS *msg)
 {
    DEST *d, *dnew, *temp_chain = NULL;
-   int i;
 
    if (jcr == NULL && msg == NULL) {
       init_last_jobs_list();
    }
 
-#ifndef HAVE_WIN32
+#if !defined(HAVE_WIN32)
    /*
     * Make sure we have fd's 0, 1, 2 open
     *  If we don't do this one of our sockets may open
@@ -176,6 +168,7 @@ init_msg(JCR *jcr, MSGS *msg)
     *
     */
    int fd;
+   int i;
    fd = open("/dev/null", O_RDONLY, 0644);
    if (fd > 2) {
       close(fd);
@@ -192,12 +185,12 @@ init_msg(JCR *jcr, MSGS *msg)
    if (msg == NULL) {
       daemon_msgs = (MSGS *)malloc(sizeof(MSGS));
       memset(daemon_msgs, 0, sizeof(MSGS));
-#ifndef WIN32
+#if !defined(HAVE_WIN32)
       for (i=1; i<=M_MAX; i++) {
          add_msg_dest(daemon_msgs, MD_STDOUT, i, NULL, NULL);
       }
 #endif
-      Dmsg1(050, "Create daemon global message resource 0x%x\n", daemon_msgs);
+      Dmsg1(050, "Create daemon global message resource %p\n", daemon_msgs);
       return;
    }
 
@@ -235,7 +228,7 @@ init_msg(JCR *jcr, MSGS *msg)
       daemon_msgs->dest_chain = temp_chain;
       memcpy(daemon_msgs->send_msg, msg->send_msg, sizeof(msg->send_msg));
    }
-   Dmsg2(250, "Copy message resource 0x%x to 0x%x\n", msg, temp_chain);
+   Dmsg2(250, "Copy message resource %p to %p\n", msg, temp_chain);
 
 }
 
@@ -257,7 +250,7 @@ void init_console_msg(const char *wd)
       console_msg_pending = 1;
    }
    close(fd);
-   con_fd = fopen(con_fname, "a+");
+   con_fd = fopen(con_fname, "a+b");
    if (!con_fd) {
       berrno be;
       Emsg2(M_ERROR, 0, _("Could not open console message file %s: ERR=%s\n"),
@@ -289,7 +282,7 @@ void add_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where, char *mai
    for (d=msg->dest_chain; d; d=d->next) {
       if (dest_code == d->dest_code && ((where == NULL && d->where == NULL) ||
                      (strcmp(where, d->where) == 0))) {
-         Dmsg4(850, "Add to existing d=%x msgtype=%d destcode=%d where=%s\n",
+         Dmsg4(850, "Add to existing d=%p msgtype=%d destcode=%d where=%s\n",
              d, msg_type, dest_code, NPRT(where));
          set_bit(msg_type, d->msg_types);
          set_bit(msg_type, msg->send_msg);  /* set msg_type bit in our local */
@@ -309,7 +302,7 @@ void add_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where, char *mai
    if (mail_cmd) {
       d->mail_cmd = bstrdup(mail_cmd);
    }
-   Dmsg5(850, "add new d=%x msgtype=%d destcode=%d where=%s mailcmd=%s\n",
+   Dmsg5(850, "add new d=%p msgtype=%d destcode=%d where=%s mailcmd=%s\n",
           d, msg_type, dest_code, NPRT(where), NPRT(d->mail_cmd));
    msg->dest_chain = d;
 }
@@ -324,11 +317,11 @@ void rem_msg_dest(MSGS *msg, int dest_code, int msg_type, char *where)
    DEST *d;
 
    for (d=msg->dest_chain; d; d=d->next) {
-      Dmsg2(850, "Remove_msg_dest d=%x where=%s\n", d, NPRT(d->where));
+      Dmsg2(850, "Remove_msg_dest d=%p where=%s\n", d, NPRT(d->where));
       if (bit_is_set(msg_type, d->msg_types) && (dest_code == d->dest_code) &&
           ((where == NULL && d->where == NULL) ||
                      (strcmp(where, d->where) == 0))) {
-         Dmsg3(850, "Found for remove d=%x msgtype=%d destcode=%d\n",
+         Dmsg3(850, "Found for remove d=%p msgtype=%d destcode=%d\n",
                d, msg_type, dest_code);
          clear_bit(msg_type, d->msg_types);
          Dmsg0(850, "Return rem_msg_dest\n");
@@ -393,7 +386,7 @@ void close_msg(JCR *jcr)
    POOLMEM *cmd, *line;
    int len, stat;
 
-   Dmsg1(850, "Close_msg jcr=0x%x\n", jcr);
+   Dmsg1(580, "Close_msg jcr=%p\n", jcr);
 
    if (jcr == NULL) {                /* NULL -> global chain */
       msgs = daemon_msgs;
@@ -405,7 +398,7 @@ void close_msg(JCR *jcr)
    if (msgs == NULL) {
       return;
    }
-   Dmsg1(850, "===Begin close msg resource at 0x%x\n", msgs);
+   Dmsg1(850, "===Begin close msg resource at %p\n", msgs);
    cmd = get_pool_memory(PM_MESSAGE);
    for (d=msgs->dest_chain; d; ) {
       if (d->fd) {
@@ -584,7 +577,7 @@ void dispatch_message(JCR *jcr, int type, time_t mtime, char *msg)
     }
 
     if (type == M_ABORT || type == M_ERROR_TERM) {
-#ifndef HAVE_WIN32
+#if !defined(HAVE_WIN32)
        fputs(dt, stdout);
        fputs(msg, stdout);         /* print this here to INSURE that it is printed */
        fflush(stdout);
@@ -602,10 +595,23 @@ void dispatch_message(JCR *jcr, int type, time_t mtime, char *msg)
     for (d=msgs->dest_chain; d; d=d->next) {
        if (bit_is_set(type, d->msg_types)) {
           switch (d->dest_code) {
+             case MD_CATALOG:
+                char ed1[50];
+                if (!jcr || !jcr->db) {
+                   break;
+                }
+                if (p_sql_query) {
+                   POOL_MEM cmd(PM_MESSAGE);
+                   bstrftimes(dt, sizeof(dt), mtime);
+                   Mmsg(cmd, "INSERT INTO Log (JobId, Time, LogText) VALUES (%s,'%s','%s')",
+                         edit_int64(jcr->JobId, ed1), dt, msg);
+                   p_sql_query(jcr, cmd.c_str());
+                }
+                break;
              case MD_CONSOLE:
                 Dmsg1(850, "CONSOLE for following msg: %s", msg);
                 if (!con_fd) {
-                   con_fd = fopen(con_fname, "a+");
+                   con_fd = fopen(con_fname, "a+b");
                    Dmsg0(850, "Console file not open.\n");
                 }
                 if (con_fd) {
@@ -629,7 +635,7 @@ void dispatch_message(JCR *jcr, int type, time_t mtime, char *msg)
                 }
                 break;
              case MD_SYSLOG:
-                Dmsg1(850, "SYSLOG for collowing msg: %s\n", msg);
+                Dmsg1(850, "SYSLOG for following msg: %s\n", msg);
                 /*
                  * We really should do an openlog() here.
                  */
@@ -660,7 +666,7 @@ void dispatch_message(JCR *jcr, int type, time_t mtime, char *msg)
                 if (!d->fd) {
                    POOLMEM *name = get_pool_memory(PM_MESSAGE);
                    make_unique_mail_filename(jcr, name, d);
-                   d->fd = fopen(name, "w+");
+                   d->fd = fopen(name, "w+b");
                    if (!d->fd) {
                       berrno be;
                       d->fd = stdout;
@@ -682,7 +688,7 @@ void dispatch_message(JCR *jcr, int type, time_t mtime, char *msg)
              case MD_FILE:
                 Dmsg1(850, "FILE for following msg: %s", msg);
                 if (!d->fd) {
-                   d->fd = fopen(d->where, "w+");
+                   d->fd = fopen(d->where, "w+b");
                    if (!d->fd) {
                       berrno be;
                       d->fd = stdout;
@@ -698,7 +704,7 @@ void dispatch_message(JCR *jcr, int type, time_t mtime, char *msg)
              case MD_APPEND:
                 Dmsg1(850, "APPEND for following msg: %s", msg);
                 if (!d->fd) {
-                   d->fd = fopen(d->where, "a");
+                   d->fd = fopen(d->where, "ab");
                    if (!d->fd) {
                       berrno be;
                       d->fd = stdout;
@@ -767,9 +773,14 @@ d_msg(const char *file, int line, int level, const char *fmt,...)
           /* visual studio passes the whole path to the file as well
            * which makes for very long lines
            */
-          const char *f = strrchr(file, '\\');
-          if (f) file = f + 1;
-          len = bsnprintf(buf, sizeof(buf), "%s: %s:%d ", my_name, file, line);
+          const char *basename;
+
+          if ((basename = strrchr(file, '\\')) == NULL) {
+             basename = file;
+          } else {
+             basename++;
+          }
+          len = bsnprintf(buf, sizeof(buf), "%s: %s:%d ", my_name, basename, line);
        } else {
           len = 0;
        }
@@ -788,7 +799,7 @@ d_msg(const char *file, int line, int level, const char *fmt,...)
           if (!trace_fd) {
              char fn[200];
              bsnprintf(fn, sizeof(fn), "%s/bacula.trace", working_directory ? working_directory : ".");
-             trace_fd = fopen(fn, "a+");
+             trace_fd = fopen(fn, "a+b");
           }
           if (trace_fd) {
              fputs(buf, trace_fd);
@@ -844,7 +855,14 @@ p_msg(const char *file, int line, int level, const char *fmt,...)
 
 #ifdef FULL_LOCATION
     if (level >= 0) {
-       len = bsnprintf(buf, sizeof(buf), "%s: %s:%d ", my_name, file, line);
+       const char *basename;
+
+       if ((basename = strrchr(file, '\\')) == NULL) {
+          basename = file;
+       } else {
+          basename++;
+       }
+       len = bsnprintf(buf, sizeof(buf), "%s: %s:%d ", my_name, basename, line);
     } else {
        len = 0;
     }
@@ -884,12 +902,19 @@ t_msg(const char *file, int line, int level, const char *fmt,...)
     if (level <= debug_level) {
        if (!trace_fd) {
           bsnprintf(buf, sizeof(buf), "%s/bacula.trace", working_directory);
-          trace_fd = fopen(buf, "a+");
+          trace_fd = fopen(buf, "a+b");
        }
 
 #ifdef FULL_LOCATION
        if (details) {
-          len = bsnprintf(buf, sizeof(buf), "%s: %s:%d ", my_name, file, line);
+          const char *basename;
+
+          if ((basename = strrchr(file, '\\')) == NULL) {
+             basename = file;
+          } else {
+             basename++;
+          }
+          len = bsnprintf(buf, sizeof(buf), "%s: %s:%d ", my_name, basename, line);
        } else {
           len = 0;
        }
@@ -920,6 +945,14 @@ e_msg(const char *file, int line, int type, int level, const char *fmt,...)
     va_list   arg_ptr;
     int len;
 
+    const char *basename;
+
+    if ((basename = strrchr(file, '\\')) == NULL) {
+       basename = file;
+    } else {
+       basename++;
+    }
+
     /*
      * Check if we have a message destination defined.
      * We always report M_ABORT and M_ERROR_TERM
@@ -931,23 +964,23 @@ e_msg(const char *file, int line, int type, int level, const char *fmt,...)
     switch (type) {
     case M_ABORT:
        len = bsnprintf(buf, sizeof(buf), _("%s: ABORTING due to ERROR in %s:%d\n"),
-               my_name, file, line);
+               my_name, basename, line);
        break;
     case M_ERROR_TERM:
        len = bsnprintf(buf, sizeof(buf), _("%s: ERROR TERMINATION at %s:%d\n"),
-               my_name, file, line);
+               my_name, basename, line);
        break;
     case M_FATAL:
        if (level == -1)            /* skip details */
           len = bsnprintf(buf, sizeof(buf), _("%s: Fatal Error because: "), my_name);
        else
-          len = bsnprintf(buf, sizeof(buf), _("%s: Fatal Error at %s:%d because:\n"), my_name, file, line);
+          len = bsnprintf(buf, sizeof(buf), _("%s: Fatal Error at %s:%d because:\n"), my_name, basename, line);
        break;
     case M_ERROR:
        if (level == -1)            /* skip details */
           len = bsnprintf(buf, sizeof(buf), _("%s: ERROR: "), my_name);
        else
-          len = bsnprintf(buf, sizeof(buf), _("%s: ERROR in %s:%d "), my_name, file, line);
+          len = bsnprintf(buf, sizeof(buf), _("%s: ERROR in %s:%d "), my_name, basename, line);
        break;
     case M_WARNING:
        len = bsnprintf(buf, sizeof(buf), _("%s: Warning: "), my_name);
@@ -1081,9 +1114,16 @@ void j_msg(const char *file, int line, JCR *jcr, int type, time_t mtime, const c
    va_list   arg_ptr;
    int i, len, maxlen;
    POOLMEM *pool_buf;
+   const char *basename;
+
+   if ((basename = strrchr(file, '\\')) == NULL) {
+      basename = file;
+   } else {
+      basename++;
+   }
 
    pool_buf = get_pool_memory(PM_EMSG);
-   i = Mmsg(pool_buf, "%s:%d ", file, line);
+   i = Mmsg(pool_buf, "%s:%d ", basename, line);
 
    for (;;) {
       maxlen = sizeof_pool_memory(pool_buf) - i - 1;
@@ -1110,7 +1150,15 @@ int m_msg(const char *file, int line, POOLMEM **pool_buf, const char *fmt, ...)
    va_list   arg_ptr;
    int i, len, maxlen;
 
-   i = sprintf(*pool_buf, "%s:%d ", file, line);
+   const char *basename;
+
+   if ((basename = strrchr(file, '\\')) == NULL) {
+      basename = file;
+   } else {
+      basename++;
+   }
+
+   i = sprintf(*pool_buf, "%s:%d ", basename, line);
 
    for (;;) {
       maxlen = sizeof_pool_memory(*pool_buf) - i - 1;
@@ -1131,7 +1179,15 @@ int m_msg(const char *file, int line, POOLMEM *&pool_buf, const char *fmt, ...)
    va_list   arg_ptr;
    int i, len, maxlen;
 
-   i = sprintf(pool_buf, "%s:%d ", file, line);
+   const char *basename;
+
+   if ((basename = strrchr(file, '\\')) == NULL) {
+      basename = file;
+   } else {
+      basename++;
+   }
+
+   i = sprintf(pool_buf, "%s:%d ", basename, line);
 
    for (;;) {
       maxlen = sizeof_pool_memory(pool_buf) - i - 1;
@@ -1253,7 +1309,6 @@ void Qmsg(JCR *jcr, int type, time_t mtime, const char *fmt,...)
       P(msg_queue_mutex);
       jcr->msg_queue->append(item);
       V(msg_queue_mutex);
-//    Dmsg1(000, "queue item=%lu\n", (long unsigned)item);
    }
    free_memory(pool_buf);
 }
@@ -1267,7 +1322,6 @@ void dequeue_messages(JCR *jcr)
    P(msg_queue_mutex);
    jcr->dequeuing = true;
    foreach_dlist(item, jcr->msg_queue) {
-//    Dmsg1(000, "dequeue item=%lu\n", (long unsigned)item);
       Jmsg(jcr, item->type, item->mtime, "%s", item->msg);
    }
    jcr->msg_queue->destroy();