1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2004-2013 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: George A. Howlett <gah@purdue.edu> |
---|
6 | */ |
---|
7 | #include <stdio.h> |
---|
8 | #include <stdarg.h> |
---|
9 | #include <syslog.h> |
---|
10 | #include <string.h> |
---|
11 | |
---|
12 | #include <string> |
---|
13 | #include <sstream> |
---|
14 | |
---|
15 | #include <GL/glew.h> |
---|
16 | |
---|
17 | #include "nanovis.h" |
---|
18 | #include "Trace.h" |
---|
19 | |
---|
20 | using namespace nv; |
---|
21 | |
---|
22 | static std::ostringstream g_UserErrorString; |
---|
23 | |
---|
24 | #define MSG_LEN 2047 |
---|
25 | |
---|
26 | /** |
---|
27 | * \brief Open syslog for writing |
---|
28 | */ |
---|
29 | void |
---|
30 | nv::initLog() |
---|
31 | { |
---|
32 | openlog("nanovis", LOG_CONS | LOG_PERROR | LOG_PID, LOG_USER); |
---|
33 | } |
---|
34 | |
---|
35 | /** |
---|
36 | * \brief Close syslog |
---|
37 | */ |
---|
38 | void |
---|
39 | nv::closeLog() |
---|
40 | { |
---|
41 | closelog(); |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * \brief Write a message to syslog |
---|
46 | */ |
---|
47 | void |
---|
48 | nv::LogMessage(int priority, const char *funcname, |
---|
49 | const char *path, int lineNum, const char* fmt, ...) |
---|
50 | { |
---|
51 | char message[MSG_LEN+1]; |
---|
52 | const char *s; |
---|
53 | int length; |
---|
54 | va_list lst; |
---|
55 | |
---|
56 | va_start(lst, fmt); |
---|
57 | s = strrchr(path, '/'); |
---|
58 | if (s == NULL) { |
---|
59 | s = path; |
---|
60 | } else { |
---|
61 | s++; |
---|
62 | } |
---|
63 | if (priority & LOG_DEBUG) { |
---|
64 | length = snprintf(message, MSG_LEN, "%s:%d(%s): ", s, lineNum, funcname); |
---|
65 | } else { |
---|
66 | length = snprintf(message, MSG_LEN, "%s:%d: ", s, lineNum); |
---|
67 | } |
---|
68 | length += vsnprintf(message + length, MSG_LEN - length, fmt, lst); |
---|
69 | message[MSG_LEN] = '\0'; |
---|
70 | |
---|
71 | syslog(priority, "%s", message); |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * \brief Write a user message to buffer |
---|
76 | */ |
---|
77 | void |
---|
78 | nv::logUserMessage(const char* fmt, ...) |
---|
79 | { |
---|
80 | char message[MSG_LEN + 1]; |
---|
81 | int length = 0; |
---|
82 | va_list lst; |
---|
83 | |
---|
84 | va_start(lst, fmt); |
---|
85 | |
---|
86 | length += vsnprintf(message, MSG_LEN, fmt, lst); |
---|
87 | message[MSG_LEN] = '\0'; |
---|
88 | |
---|
89 | g_UserErrorString << message << "\n"; |
---|
90 | } |
---|
91 | |
---|
92 | const char * |
---|
93 | nv::getUserMessages() |
---|
94 | { |
---|
95 | return g_UserErrorString.str().c_str(); |
---|
96 | } |
---|
97 | |
---|
98 | void |
---|
99 | nv::clearUserMessages() |
---|
100 | { |
---|
101 | g_UserErrorString.str(std::string()); |
---|
102 | } |
---|
103 | |
---|
104 | bool |
---|
105 | CheckFBO(GLenum *status) |
---|
106 | { |
---|
107 | *status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); |
---|
108 | return (*status == GL_FRAMEBUFFER_COMPLETE_EXT); |
---|
109 | } |
---|
110 | |
---|
111 | void |
---|
112 | PrintFBOStatus(GLenum status, const char *prefix) |
---|
113 | { |
---|
114 | #ifdef WANT_TRACE |
---|
115 | const char *mesg; |
---|
116 | |
---|
117 | switch(status) { |
---|
118 | case GL_FRAMEBUFFER_COMPLETE_EXT: |
---|
119 | mesg = "<<<< OK >>>>"; break; |
---|
120 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT: |
---|
121 | mesg = "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT"; break; |
---|
122 | case GL_FRAMEBUFFER_UNSUPPORTED_EXT: |
---|
123 | mesg = "GL_FRAMEBUFFER_UNSUPPORTED_EXT"; break; |
---|
124 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT: |
---|
125 | mesg = "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT"; break; |
---|
126 | case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT: |
---|
127 | mesg = "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT"; break; |
---|
128 | case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT: |
---|
129 | mesg = "GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT"; break; |
---|
130 | case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT: |
---|
131 | mesg = "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT"; break; |
---|
132 | case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT: |
---|
133 | mesg = "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT"; break; |
---|
134 | default: |
---|
135 | TRACE("FB Status: %s: UNKNOWN framebuffer status %u", |
---|
136 | prefix, (unsigned int)status); |
---|
137 | return; |
---|
138 | } |
---|
139 | TRACE("FB Status: %s: %s", prefix, mesg); |
---|
140 | #endif /*WANT_TRACE*/ |
---|
141 | } |
---|
142 | |
---|
143 | bool |
---|
144 | CheckGL(const char *prefix) |
---|
145 | { |
---|
146 | GLenum status = (GLenum)glGetError(); |
---|
147 | if (status == GL_NO_ERROR) { |
---|
148 | return true; |
---|
149 | } |
---|
150 | #ifdef WANT_TRACE |
---|
151 | const char *mesg; |
---|
152 | |
---|
153 | switch(status) { |
---|
154 | case GL_INVALID_ENUM: |
---|
155 | mesg = "GL_INVALID_ENUM"; break; |
---|
156 | case GL_INVALID_VALUE: |
---|
157 | mesg = "GL_INVALID_VALUE"; break; |
---|
158 | case GL_INVALID_OPERATION: |
---|
159 | mesg = "GL_INVALID_OPERATION"; break; |
---|
160 | case GL_STACK_OVERFLOW: |
---|
161 | mesg = "GL_STACK_OVERFLOW"; break; |
---|
162 | case GL_STACK_UNDERFLOW: |
---|
163 | mesg = "GL_STACK_UNDERFLOW"; break; |
---|
164 | case GL_OUT_OF_MEMORY: |
---|
165 | mesg = "GL_OUT_OF_MEMORY"; break; |
---|
166 | case GL_INVALID_FRAMEBUFFER_OPERATION_EXT: |
---|
167 | mesg = "GL_INVALID_FRAMEBUFFER_OPERATION_EXT"; break; |
---|
168 | default: |
---|
169 | TRACE("GL Status: %s: Unknown status %d", prefix, status); |
---|
170 | return false; |
---|
171 | } |
---|
172 | TRACE("GL Status: %s: %s", prefix, mesg); |
---|
173 | #endif |
---|
174 | return false; |
---|
175 | } |
---|