Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * Project: PROJ.4 |
3 | | * Purpose: Implementation of pj_log() function. |
4 | | * Author: Frank Warmerdam, warmerdam@pobox.com |
5 | | * |
6 | | ****************************************************************************** |
7 | | * Copyright (c) 2010, Frank Warmerdam |
8 | | * |
9 | | * Permission is hereby granted, free of charge, to any person obtaining a |
10 | | * copy of this software and associated documentation files (the "Software"), |
11 | | * to deal in the Software without restriction, including without limitation |
12 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
13 | | * and/or sell copies of the Software, and to permit persons to whom the |
14 | | * Software is furnished to do so, subject to the following conditions: |
15 | | * |
16 | | * The above copyright notice and this permission notice shall be included |
17 | | * in all copies or substantial portions of the Software. |
18 | | * |
19 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
20 | | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
22 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
24 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
25 | | * DEALINGS IN THE SOFTWARE. |
26 | | *****************************************************************************/ |
27 | | |
28 | | #include <stdarg.h> |
29 | | #include <stdio.h> |
30 | | #include <stdlib.h> |
31 | | #include <string.h> |
32 | | |
33 | | #include "proj.h" |
34 | | #include "proj_internal.h" |
35 | | |
36 | | /************************************************************************/ |
37 | | /* pj_stderr_logger() */ |
38 | | /************************************************************************/ |
39 | | |
40 | | void pj_stderr_logger(void *app_data, int level, const char *msg) |
41 | | |
42 | 50.3k | { |
43 | 50.3k | (void)app_data; |
44 | 50.3k | (void)level; |
45 | 50.3k | fprintf(stderr, "%s\n", msg); |
46 | 50.3k | } |
47 | | |
48 | | /************************************************************************/ |
49 | | /* pj_log_active() */ |
50 | | /************************************************************************/ |
51 | | |
52 | 2.13M | bool pj_log_active(PJ_CONTEXT *ctx, int level) { |
53 | 2.13M | int debug_level = ctx->debug_level; |
54 | 2.13M | int shutup_unless_errno_set = debug_level < 0; |
55 | | |
56 | | /* For negative debug levels, we first start logging when errno is set */ |
57 | 2.13M | if (ctx->last_errno == 0 && shutup_unless_errno_set) |
58 | 0 | return false; |
59 | | |
60 | 2.13M | if (debug_level < 0) |
61 | 0 | debug_level = -debug_level; |
62 | | |
63 | 2.13M | if (level > debug_level) |
64 | 2.09M | return false; |
65 | 45.4k | return true; |
66 | 2.13M | } |
67 | | |
68 | | /************************************************************************/ |
69 | | /* pj_vlog() */ |
70 | | /************************************************************************/ |
71 | | |
72 | | static void pj_vlog(PJ_CONTEXT *ctx, int level, const PJ *P, const char *fmt, |
73 | | va_list args) |
74 | | |
75 | 2.13M | { |
76 | 2.13M | char *msg_buf; |
77 | 2.13M | if (!pj_log_active(ctx, level)) |
78 | 2.09M | return; |
79 | | |
80 | 45.4k | constexpr size_t BUF_SIZE = 100000; |
81 | 45.4k | msg_buf = (char *)malloc(BUF_SIZE); |
82 | 45.4k | if (msg_buf == nullptr) |
83 | 0 | return; |
84 | | |
85 | 45.4k | if (P == nullptr || P->short_name == nullptr) |
86 | 3.09k | vsnprintf(msg_buf, BUF_SIZE, fmt, args); |
87 | 42.3k | else { |
88 | 42.3k | std::string fmt_with_P_short_name(P->short_name); |
89 | 42.3k | fmt_with_P_short_name += ": "; |
90 | 42.3k | fmt_with_P_short_name += fmt; |
91 | 42.3k | vsnprintf(msg_buf, BUF_SIZE, fmt_with_P_short_name.c_str(), args); |
92 | 42.3k | } |
93 | 45.4k | msg_buf[BUF_SIZE - 1] = '\0'; |
94 | | |
95 | 45.4k | ctx->logger(ctx->logger_app_data, level, msg_buf); |
96 | | |
97 | 45.4k | free(msg_buf); |
98 | 45.4k | } |
99 | | |
100 | | /************************************************************************/ |
101 | | /* pj_log() */ |
102 | | /************************************************************************/ |
103 | | |
104 | | void pj_log(PJ_CONTEXT *ctx, int level, const char *fmt, ...) |
105 | | |
106 | 87.5k | { |
107 | 87.5k | va_list args; |
108 | | |
109 | 87.5k | if (level > ctx->debug_level) |
110 | 84.4k | return; |
111 | | |
112 | 3.07k | va_start(args, fmt); |
113 | 3.07k | pj_vlog(ctx, level, nullptr, fmt, args); |
114 | 3.07k | va_end(args); |
115 | 3.07k | } |
116 | | |
117 | | /***************************************************************************************/ |
118 | 136k | PJ_LOG_LEVEL proj_log_level(PJ_CONTEXT *ctx, PJ_LOG_LEVEL log_level) { |
119 | | /**************************************************************************************** |
120 | | Set logging level 0-3. Higher number means more debug info. 0 turns it |
121 | | off |
122 | | ****************************************************************************************/ |
123 | 136k | PJ_LOG_LEVEL previous; |
124 | 136k | if (nullptr == ctx) |
125 | 0 | ctx = pj_get_default_ctx(); |
126 | 136k | if (nullptr == ctx) |
127 | 0 | return PJ_LOG_TELL; |
128 | 136k | previous = static_cast<PJ_LOG_LEVEL>(abs(ctx->debug_level)); |
129 | 136k | if (PJ_LOG_TELL == log_level) |
130 | 111k | return previous; |
131 | 24.9k | ctx->debug_level = log_level; |
132 | 24.9k | return previous; |
133 | 136k | } |
134 | | |
135 | | /*****************************************************************************/ |
136 | 42.4k | void proj_log_error(const PJ *P, const char *fmt, ...) { |
137 | | /****************************************************************************** |
138 | | For reporting the most severe events. |
139 | | ******************************************************************************/ |
140 | 42.4k | va_list args; |
141 | 42.4k | va_start(args, fmt); |
142 | 42.4k | pj_vlog(pj_get_ctx((PJ *)P), PJ_LOG_ERROR, P, fmt, args); |
143 | 42.4k | va_end(args); |
144 | 42.4k | } |
145 | | |
146 | | /*****************************************************************************/ |
147 | 0 | void proj_log_debug(PJ *P, const char *fmt, ...) { |
148 | | /****************************************************************************** |
149 | | For reporting debugging information. |
150 | | ******************************************************************************/ |
151 | 0 | va_list args; |
152 | 0 | va_start(args, fmt); |
153 | 0 | pj_vlog(pj_get_ctx(P), PJ_LOG_DEBUG, P, fmt, args); |
154 | 0 | va_end(args); |
155 | 0 | } |
156 | | |
157 | | /*****************************************************************************/ |
158 | 7.12k | void proj_context_log_debug(PJ_CONTEXT *ctx, const char *fmt, ...) { |
159 | | /****************************************************************************** |
160 | | For reporting debugging information. |
161 | | ******************************************************************************/ |
162 | 7.12k | va_list args; |
163 | 7.12k | va_start(args, fmt); |
164 | 7.12k | pj_vlog(ctx, PJ_LOG_DEBUG, nullptr, fmt, args); |
165 | 7.12k | va_end(args); |
166 | 7.12k | } |
167 | | |
168 | | /*****************************************************************************/ |
169 | 2.08M | void proj_log_trace(PJ *P, const char *fmt, ...) { |
170 | | /****************************************************************************** |
171 | | For reporting embarrassingly detailed debugging information. |
172 | | ******************************************************************************/ |
173 | 2.08M | va_list args; |
174 | 2.08M | va_start(args, fmt); |
175 | 2.08M | pj_vlog(pj_get_ctx(P), PJ_LOG_TRACE, P, fmt, args); |
176 | 2.08M | va_end(args); |
177 | 2.08M | } |
178 | | |
179 | | /*****************************************************************************/ |
180 | 12.4k | void proj_log_func(PJ_CONTEXT *ctx, void *app_data, PJ_LOG_FUNCTION logf) { |
181 | | /****************************************************************************** |
182 | | Put a new logging function into P's context. The opaque object app_data |
183 | | is passed as first arg at each call to the logger |
184 | | ******************************************************************************/ |
185 | 12.4k | if (nullptr == ctx) |
186 | 0 | ctx = pj_get_default_ctx(); |
187 | 12.4k | ctx->logger_app_data = app_data; |
188 | 12.4k | if (nullptr != logf) |
189 | 12.4k | ctx->logger = logf; |
190 | 12.4k | } |