blob: 902e7680a5f2f4190522fd14546450daee1c9aeb [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003-2005 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26#include <stdarg.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include "Trace.h"
30
31static int j2dTraceLevel = J2D_TRACE_INVALID;
32static FILE *j2dTraceFile = NULL;
33
34JNIEXPORT void JNICALL
35J2dTraceImpl(int level, jboolean cr, const char *string, ...)
36{
37 va_list args;
38 if (j2dTraceLevel < J2D_TRACE_OFF) {
39 J2dTraceInit();
40 }
41 if (level <= j2dTraceLevel) {
42 if (cr) {
43 switch (level) {
44 case J2D_TRACE_ERROR:
45 fprintf(j2dTraceFile, "[E] ");
46 break;
47 case J2D_TRACE_WARNING:
48 fprintf(j2dTraceFile, "[W] ");
49 break;
50 case J2D_TRACE_INFO:
51 fprintf(j2dTraceFile, "[I] ");
52 break;
53 case J2D_TRACE_VERBOSE:
54 fprintf(j2dTraceFile, "[V] ");
55 break;
56 case J2D_TRACE_VERBOSE2:
57 fprintf(j2dTraceFile, "[X] ");
58 break;
59 default:
60 break;
61 }
62 }
63
64 va_start(args, string);
65 vfprintf(j2dTraceFile, string, args);
66 va_end(args);
67
68 if (cr) {
69 fprintf(j2dTraceFile, "\n");
70 }
71 fflush(j2dTraceFile);
72 }
73}
74
75JNIEXPORT void JNICALL
76J2dTraceInit()
77{
78 char *j2dTraceLevelString = getenv("J2D_TRACE_LEVEL");
79 char *j2dTraceFileName;
80 j2dTraceLevel = J2D_TRACE_OFF;
81 if (j2dTraceLevelString) {
82 int traceLevelTmp = -1;
83 int args = sscanf(j2dTraceLevelString, "%d", &traceLevelTmp);
84 if (args > 0 &&
85 traceLevelTmp > J2D_TRACE_INVALID &&
86 traceLevelTmp < J2D_TRACE_MAX)
87 {
88 j2dTraceLevel = traceLevelTmp;
89 }
90 }
91 j2dTraceFileName = getenv("J2D_TRACE_FILE");
92 if (j2dTraceFileName) {
93 j2dTraceFile = fopen(j2dTraceFileName, "w");
94 if (!j2dTraceFile) {
95 printf("[E]: Error opening trace file %s\n", j2dTraceFileName);
96 }
97 }
98 if (!j2dTraceFile) {
99 j2dTraceFile = stdout;
100 }
101}