source: vtkvis/trunk/Trace.cpp @ 4641

Last change on this file since 4641 was 4422, checked in by ldelgass, 10 years ago

Fix crash bug in user errors: was returning pointer to temporary. Return a
copy of string object from getUserMessages instead of pointer.

  • Property svn:eol-style set to native
File size: 1.8 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (C) 2004-2012  HUBzero Foundation, LLC
4 *
5 * Author: George A. Howlett <gah@purdue.edu>
6 */
7
8#include <cstdio>
9#include <cstdarg>
10#include <cstring>
11#include <syslog.h>
12
13#include <string>
14#include <sstream>
15
16#include "Trace.h"
17
18using namespace VtkVis;
19
20static std::ostringstream g_UserErrorString;
21
22#define MSG_LEN 2047
23
24/**
25 * \brief Open syslog for writing
26 */
27void
28VtkVis::initLog()
29{
30    openlog("vtkvis", LOG_CONS | LOG_PERROR | LOG_PID,  LOG_USER);
31}
32
33/**
34 * \brief Close syslog
35 */
36void
37VtkVis::closeLog()
38{
39    closelog();
40}
41
42/**
43 * \brief Write a message to syslog
44 */
45void
46VtkVis::logMessage(int priority, const char *funcname,
47                   const char *path, int lineNum, const char* fmt, ...)
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
69    syslog(priority, "%s", message);
70}
71
72/**
73 * \brief Write a user message to buffer
74 */
75void
76VtkVis::logUserMessage(const char* fmt, ...)
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
90std::string
91VtkVis::getUserMessages()
92{
93    return g_UserErrorString.str();
94}
95
96void
97VtkVis::clearUserMessages()
98{
99    g_UserErrorString.str(std::string());
100}
Note: See TracBrowser for help on using the repository browser.