[2100] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
| 2 | /* |
---|
[3177] | 3 | * Copyright (C) 2004-2012 HUBzero Foundation, LLC |
---|
[2100] | 4 | * |
---|
[2506] | 5 | * Author: George A. Howlett <gah@purdue.edu> |
---|
[2100] | 6 | */ |
---|
| 7 | |
---|
| 8 | #include <cstdio> |
---|
| 9 | #include <cstdarg> |
---|
| 10 | #include <cstring> |
---|
| 11 | #include <syslog.h> |
---|
| 12 | |
---|
[3360] | 13 | #include <string> |
---|
| 14 | #include <sstream> |
---|
| 15 | |
---|
[2100] | 16 | #include "Trace.h" |
---|
| 17 | |
---|
[3615] | 18 | using namespace VtkVis; |
---|
[2100] | 19 | |
---|
[3360] | 20 | static std::ostringstream g_UserErrorString; |
---|
| 21 | |
---|
[2100] | 22 | #define MSG_LEN 2047 |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * \brief Open syslog for writing |
---|
| 26 | */ |
---|
| 27 | void |
---|
[3615] | 28 | VtkVis::initLog() |
---|
[2100] | 29 | { |
---|
| 30 | openlog("vtkvis", LOG_CONS | LOG_PERROR | LOG_PID, LOG_USER); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * \brief Close syslog |
---|
| 35 | */ |
---|
| 36 | void |
---|
[3615] | 37 | VtkVis::closeLog() |
---|
[2100] | 38 | { |
---|
| 39 | closelog(); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * \brief Write a message to syslog |
---|
| 44 | */ |
---|
| 45 | void |
---|
[3615] | 46 | VtkVis::logMessage(int priority, const char *funcname, |
---|
| 47 | const char *path, int lineNum, const char* fmt, ...) |
---|
[2100] | 48 | { |
---|
| 49 | char message[MSG_LEN + 1]; |
---|
| 50 | const char *s; |
---|
| 51 | int length; |
---|
| 52 | va_list lst; |
---|
| 53 | |
---|
| 54 | va_start(lst, fmt); |
---|
| 55 | s = strrchr(path, '/'); |
---|
| 56 | if (s == NULL) { |
---|
| 57 | s = path; |
---|
| 58 | } else { |
---|
| 59 | s++; |
---|
| 60 | } |
---|
| 61 | if (priority & LOG_DEBUG) { |
---|
| 62 | length = snprintf(message, MSG_LEN, "%s:%d(%s): ", s, lineNum, funcname); |
---|
| 63 | } else { |
---|
| 64 | length = snprintf(message, MSG_LEN, "%s:%d: ", s, lineNum); |
---|
| 65 | } |
---|
| 66 | length += vsnprintf(message + length, MSG_LEN - length, fmt, lst); |
---|
| 67 | message[MSG_LEN] = '\0'; |
---|
| 68 | |
---|
[3330] | 69 | syslog(priority, "%s", message); |
---|
[2100] | 70 | } |
---|
[3360] | 71 | |
---|
| 72 | /** |
---|
| 73 | * \brief Write a user message to buffer |
---|
| 74 | */ |
---|
| 75 | void |
---|
[3615] | 76 | VtkVis::logUserMessage(const char* fmt, ...) |
---|
[3360] | 77 | { |
---|
| 78 | char message[MSG_LEN + 1]; |
---|
| 79 | int length = 0; |
---|
| 80 | va_list lst; |
---|
| 81 | |
---|
| 82 | va_start(lst, fmt); |
---|
| 83 | |
---|
| 84 | length += vsnprintf(message, MSG_LEN, fmt, lst); |
---|
| 85 | message[MSG_LEN] = '\0'; |
---|
| 86 | |
---|
| 87 | g_UserErrorString << message << "\n"; |
---|
| 88 | } |
---|
| 89 | |
---|
[4605] | 90 | std::string |
---|
[3615] | 91 | VtkVis::getUserMessages() |
---|
[3360] | 92 | { |
---|
[4605] | 93 | return g_UserErrorString.str(); |
---|
[3360] | 94 | } |
---|
| 95 | |
---|
| 96 | void |
---|
[3615] | 97 | VtkVis::clearUserMessages() |
---|
[3360] | 98 | { |
---|
| 99 | g_UserErrorString.str(std::string()); |
---|
| 100 | } |
---|