Implemented WriteLog() under UNIX
authorVictor Kirhenshtein <victor@netxms.org>
Fri, 14 May 2004 14:06:51 +0000 (14:06 +0000)
committerVictor Kirhenshtein <victor@netxms.org>
Fri, 14 May 2004 14:06:51 +0000 (14:06 +0000)
src/server/core/log.cpp

index 3bc8f71..c4841d2 100644 (file)
 #include "nms_core.h"
 
 
+//
+// Messages generated by mc.pl (for UNIX version only)
+//
+
+#ifndef _WIN32
+extern DWORD g_dwNumMessages;
+extern char *g_szMessages[];
+#endif
+
+
 //
 // Static data
 //
@@ -113,6 +123,61 @@ static void WriteLogToFile(char *szMessage)
 }
 
 
+//
+// Format message (UNIX version)
+//
+
+#ifndef _WIN32
+
+char *FormatMessageUX(DWORD dwMsgId, char **ppStrings)
+{
+   char *p, *pMsg;
+   int i, iSize, iLen;
+
+   if (dwMsgId >= g_dwNumMessages)
+   {
+      // No message with this ID
+      pMsg = (char *)malloc(128);
+      sprintf(pMsg, "MSG 0x%08X - Unable to find message text\n", dwMsgId);
+   }
+   else
+   {
+      iSize = strlen(g_szMessages[dwMsgId]) + 2;
+      pMsg = (char *)malloc(iSize);
+
+      for(i = 0, p = g_szMessages[dwMsgId]; *p != 0; p++)
+         if (*p == '%')
+         {
+            p++;
+            if ((*p >= '1') && (*p <= '9'))
+            {
+               iLen = strlen(ppStrings[*p - '1']);
+               iSize += iLen;
+               pMsg = (char *)realloc(pMsg, iSize);
+               strcpy(&pMsg[i], ppStrings[*p - '1']);
+               i += iLen;
+            }
+            else
+            {
+               if (*p == 0)   // Handle single % character at the string end
+                  break;
+               pMsg[i++] = *p;
+            }
+         }
+         else
+         {
+            pMsg[i++] = *p;
+         }
+      pMsg[i++] = '\n';
+      pMsg[i] = 0;
+   }
+
+   return pMsg;
+}
+
+#endif   /* ! _WIN32 */
+
+
 //
 // Write log record
 // Parameters:
@@ -130,7 +195,7 @@ static void WriteLogToFile(char *szMessage)
 void WriteLog(DWORD msg, WORD wType, char *format, ...)
 {
    va_list args;
-   char *strings[16], *msgBuf;
+   char *strings[16], *pMsg;
    int numStrings = 0;
    DWORD error;
 
@@ -167,12 +232,12 @@ void WriteLog(DWORD msg, WORD wType, char *format, ...)
                                  FORMAT_MESSAGE_IGNORE_INSERTS,
                                  NULL, error,
                                  MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), // Default language
-                                 (LPSTR)&msgBuf,0,NULL)>0)
+                                 (LPSTR)&pMsg,0,NULL)>0)
                {
-                  msgBuf[strcspn(msgBuf,"\r\n")] = 0;
-                  strings[numStrings]=(char *)malloc(strlen(msgBuf) + 1);
-                  strcpy(strings[numStrings], msgBuf);
-                  LocalFree(msgBuf);
+                  pMsg[strcspn(msgBuf,"\r\n")] = 0;
+                  strings[numStrings]=(char *)malloc(strlen(pMsg) + 1);
+                  strcpy(strings[numStrings], pMsg);
+                  LocalFree(pMsg);
                }
                else
                {
@@ -216,9 +281,15 @@ void WriteLog(DWORD msg, WORD wType, char *format, ...)
       }
    }
 #else  /* _WIN32 */
-
-   /* TODO: add event logging under UNIX */
-
+   pMsg = FormatMessageUX(msg, strings);
+   if (g_dwFlags & AF_USE_EVENT_LOG)
+   {
+   }
+   else
+   {
+      WriteLogToFile(pMsg);
+   }
+   free(pMsg);
 #endif /* _WIN32 */
 
    while(--numStrings >= 0)