blob: 58f8f58df5079b950a8276b818eb6a42764776f1 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#if defined(WEBRTC_WIN)
Tommi23edcff2015-05-25 10:45:43 +020012#if !defined(WIN32_LEAN_AND_MEAN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013#define WIN32_LEAN_AND_MEAN
Tommi23edcff2015-05-25 10:45:43 +020014#endif
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include <windows.h>
16#define snprintf _snprintf
17#undef ERROR // wingdi.h
18#endif
19
20#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
21#include <CoreServices/CoreServices.h>
22#elif defined(WEBRTC_ANDROID)
23#include <android/log.h>
24static const char kLibjingle[] = "libjingle";
25// Android has a 1024 limit on log inputs. We use 60 chars as an
26// approx for the header/tag portion.
27// See android/system/core/liblog/logd_write.c
28static const int kMaxLogLineSize = 1024 - 60;
29#endif // WEBRTC_MAC && !defined(WEBRTC_IOS) || WEBRTC_ANDROID
30
31#include <time.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032#include <limits.h>
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000033
34#include <algorithm>
35#include <iomanip>
36#include <ostream>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000037#include <vector>
38
39#include "webrtc/base/logging.h"
henrikaba35d052015-07-14 17:04:08 +020040#include "webrtc/base/platform_thread.h"
Tommi0eefb4d2015-05-23 09:54:07 +020041#include "webrtc/base/scoped_ptr.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042#include "webrtc/base/stringencode.h"
43#include "webrtc/base/stringutils.h"
44#include "webrtc/base/timeutils.h"
45
46namespace rtc {
47
48/////////////////////////////////////////////////////////////////////////////
49// Constant Labels
50/////////////////////////////////////////////////////////////////////////////
51
52const char * FindLabel(int value, const ConstantLabel entries[]) {
53 for (int i = 0; entries[i].label; ++i) {
54 if (value == entries[i].value) {
55 return entries[i].label;
56 }
57 }
58 return 0;
59}
60
61std::string ErrorName(int err, const ConstantLabel * err_table) {
62 if (err == 0)
63 return "No error";
64
65 if (err_table != 0) {
66 if (const char * value = FindLabel(err, err_table))
67 return value;
68 }
69
70 char buffer[16];
71 snprintf(buffer, sizeof(buffer), "0x%08x", err);
72 return buffer;
73}
74
75/////////////////////////////////////////////////////////////////////////////
76// LogMessage
77/////////////////////////////////////////////////////////////////////////////
78
Tommi0eefb4d2015-05-23 09:54:07 +020079// By default, release builds don't log, debug builds at info level
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080#if _DEBUG
Tommi0eefb4d2015-05-23 09:54:07 +020081LoggingSeverity LogMessage::min_sev_ = LS_INFO;
82LoggingSeverity LogMessage::dbg_sev_ = LS_INFO;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083#else // !_DEBUG
Tommi0eefb4d2015-05-23 09:54:07 +020084LoggingSeverity LogMessage::min_sev_ = LS_NONE;
85LoggingSeverity LogMessage::dbg_sev_ = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000086#endif // !_DEBUG
87
88// Global lock for log subsystem, only needed to serialize access to streams_.
89CriticalSection LogMessage::crit_;
90
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091// The list of logging streams currently configured.
92// Note: we explicitly do not clean this up, because of the uncertain ordering
93// of destructors at program exit. Let the person who sets the stream trigger
94// cleanup by setting to NULL, or let it leak (safe at program exit).
Tommi00aac5a2015-05-25 11:25:59 +020095LogMessage::StreamList LogMessage::streams_ GUARDED_BY(LogMessage::crit_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096
97// Boolean options default to false (0)
98bool LogMessage::thread_, LogMessage::timestamp_;
99
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100LogMessage::LogMessage(const char* file, int line, LoggingSeverity sev,
101 LogErrorContext err_ctx, int err, const char* module)
102 : severity_(sev),
103 warn_slow_logs_delay_(WARN_SLOW_LOGS_DELAY) {
104 if (timestamp_) {
105 uint32 time = TimeSince(LogStartTime());
106 // Also ensure WallClockStartTime is initialized, so that it matches
107 // LogStartTime.
108 WallClockStartTime();
109 print_stream_ << "[" << std::setfill('0') << std::setw(3) << (time / 1000)
110 << ":" << std::setw(3) << (time % 1000) << std::setfill(' ')
111 << "] ";
112 }
113
114 if (thread_) {
henrikaba35d052015-07-14 17:04:08 +0200115 PlatformThreadId id = CurrentThreadId();
116 print_stream_ << "[" << std::dec << id << "] ";
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000117 }
118
119 if (err_ctx != ERRCTX_NONE) {
120 std::ostringstream tmp;
121 tmp << "[0x" << std::setfill('0') << std::hex << std::setw(8) << err << "]";
122 switch (err_ctx) {
123 case ERRCTX_ERRNO:
124 tmp << " " << strerror(err);
125 break;
126#if WEBRTC_WIN
127 case ERRCTX_HRESULT: {
128 char msgbuf[256];
129 DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM;
130 HMODULE hmod = GetModuleHandleA(module);
131 if (hmod)
132 flags |= FORMAT_MESSAGE_FROM_HMODULE;
133 if (DWORD len = FormatMessageA(
134 flags, hmod, err,
135 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
136 msgbuf, sizeof(msgbuf) / sizeof(msgbuf[0]), NULL)) {
137 while ((len > 0) &&
138 isspace(static_cast<unsigned char>(msgbuf[len-1]))) {
139 msgbuf[--len] = 0;
140 }
141 tmp << " " << msgbuf;
142 }
143 break;
144 }
Tommi0eefb4d2015-05-23 09:54:07 +0200145#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000146#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS)
147 case ERRCTX_OSSTATUS: {
148 tmp << " " << nonnull(GetMacOSStatusErrorString(err), "Unknown error");
149 if (const char* desc = GetMacOSStatusCommentString(err)) {
150 tmp << ": " << desc;
151 }
152 break;
153 }
154#endif // WEBRTC_MAC && !defined(WEBRTC_IOS)
155 default:
156 break;
157 }
158 extra_ = tmp.str();
159 }
160}
161
162LogMessage::~LogMessage() {
163 if (!extra_.empty())
164 print_stream_ << " : " << extra_;
165 print_stream_ << std::endl;
166
167 const std::string& str = print_stream_.str();
168 if (severity_ >= dbg_sev_) {
169 OutputToDebug(str, severity_);
170 }
171
172 uint32 before = Time();
173 // Must lock streams_ before accessing
174 CritScope cs(&crit_);
175 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
176 if (severity_ >= it->second) {
Tommi0eefb4d2015-05-23 09:54:07 +0200177 it->first->OnLogMessage(str);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000178 }
179 }
180 uint32 delay = TimeSince(before);
181 if (delay >= warn_slow_logs_delay_) {
182 LogMessage slow_log_warning =
183 rtc::LogMessage(__FILE__, __LINE__, LS_WARNING);
184 // If our warning is slow, we don't want to warn about it, because
185 // that would lead to inifinite recursion. So, give a really big
186 // number for the delay threshold.
187 slow_log_warning.warn_slow_logs_delay_ = UINT_MAX;
188 slow_log_warning.stream() << "Slow log: took " << delay << "ms to write "
189 << str.size() << " bytes.";
190 }
191}
192
193uint32 LogMessage::LogStartTime() {
194 static const uint32 g_start = Time();
195 return g_start;
196}
197
198uint32 LogMessage::WallClockStartTime() {
199 static const uint32 g_start_wallclock = time(NULL);
200 return g_start_wallclock;
201}
202
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000203void LogMessage::LogThreads(bool on) {
204 thread_ = on;
205}
206
207void LogMessage::LogTimestamps(bool on) {
208 timestamp_ = on;
209}
210
Tommi0eefb4d2015-05-23 09:54:07 +0200211void LogMessage::LogToDebug(LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000212 dbg_sev_ = min_sev;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000213 CritScope cs(&crit_);
Tommi00aac5a2015-05-25 11:25:59 +0200214 UpdateMinLogSeverity();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000215}
216
Tommi0eefb4d2015-05-23 09:54:07 +0200217int LogMessage::GetLogToStream(LogSink* stream) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000218 CritScope cs(&crit_);
Tommi0eefb4d2015-05-23 09:54:07 +0200219 LoggingSeverity sev = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000220 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
221 if (!stream || stream == it->first) {
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000222 sev = std::min(sev, it->second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000223 }
224 }
225 return sev;
226}
227
Tommi0eefb4d2015-05-23 09:54:07 +0200228void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000229 CritScope cs(&crit_);
230 streams_.push_back(std::make_pair(stream, min_sev));
231 UpdateMinLogSeverity();
232}
233
Tommi0eefb4d2015-05-23 09:54:07 +0200234void LogMessage::RemoveLogToStream(LogSink* stream) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000235 CritScope cs(&crit_);
236 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
237 if (stream == it->first) {
238 streams_.erase(it);
239 break;
240 }
241 }
242 UpdateMinLogSeverity();
243}
244
Tommi0eefb4d2015-05-23 09:54:07 +0200245void LogMessage::ConfigureLogging(const char* params) {
246 LoggingSeverity current_level = LS_VERBOSE;
247 LoggingSeverity debug_level = GetLogToDebug();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000248
249 std::vector<std::string> tokens;
250 tokenize(params, ' ', &tokens);
251
Tommi0eefb4d2015-05-23 09:54:07 +0200252 for (const std::string& token : tokens) {
253 if (token.empty())
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000254 continue;
255
256 // Logging features
Tommi0eefb4d2015-05-23 09:54:07 +0200257 if (token == "tstamp") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000258 LogTimestamps();
Tommi0eefb4d2015-05-23 09:54:07 +0200259 } else if (token == "thread") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000260 LogThreads();
261
262 // Logging levels
Tommi0eefb4d2015-05-23 09:54:07 +0200263 } else if (token == "sensitive") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000264 current_level = LS_SENSITIVE;
Tommi0eefb4d2015-05-23 09:54:07 +0200265 } else if (token == "verbose") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000266 current_level = LS_VERBOSE;
Tommi0eefb4d2015-05-23 09:54:07 +0200267 } else if (token == "info") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000268 current_level = LS_INFO;
Tommi0eefb4d2015-05-23 09:54:07 +0200269 } else if (token == "warning") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000270 current_level = LS_WARNING;
Tommi0eefb4d2015-05-23 09:54:07 +0200271 } else if (token == "error") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000272 current_level = LS_ERROR;
Tommi0eefb4d2015-05-23 09:54:07 +0200273 } else if (token == "none") {
274 current_level = LS_NONE;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000275
276 // Logging targets
Tommi0eefb4d2015-05-23 09:54:07 +0200277 } else if (token == "debug") {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000278 debug_level = current_level;
279 }
280 }
281
282#if defined(WEBRTC_WIN)
Tommi0eefb4d2015-05-23 09:54:07 +0200283 if ((LS_NONE != debug_level) && !::IsDebuggerPresent()) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000284 // First, attempt to attach to our parent's console... so if you invoke
285 // from the command line, we'll see the output there. Otherwise, create
286 // our own console window.
287 // Note: These methods fail if a console already exists, which is fine.
288 bool success = false;
289 typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD);
290 if (HINSTANCE kernel32 = ::LoadLibrary(L"kernel32.dll")) {
291 // AttachConsole is defined on WinXP+.
292 if (PFN_AttachConsole attach_console = reinterpret_cast<PFN_AttachConsole>
293 (::GetProcAddress(kernel32, "AttachConsole"))) {
294 success = (FALSE != attach_console(ATTACH_PARENT_PROCESS));
295 }
296 ::FreeLibrary(kernel32);
297 }
298 if (!success) {
299 ::AllocConsole();
300 }
301 }
Tommi0eefb4d2015-05-23 09:54:07 +0200302#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000303
304 LogToDebug(debug_level);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000305}
306
Tommi00aac5a2015-05-25 11:25:59 +0200307void LogMessage::UpdateMinLogSeverity() EXCLUSIVE_LOCKS_REQUIRED(crit_) {
Tommi0eefb4d2015-05-23 09:54:07 +0200308 LoggingSeverity min_sev = dbg_sev_;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000309 for (StreamList::iterator it = streams_.begin(); it != streams_.end(); ++it) {
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000310 min_sev = std::min(dbg_sev_, it->second);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000311 }
312 min_sev_ = min_sev;
313}
314
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000315void LogMessage::OutputToDebug(const std::string& str,
316 LoggingSeverity severity) {
317 bool log_to_stderr = true;
318#if defined(WEBRTC_MAC) && !defined(WEBRTC_IOS) && (!defined(DEBUG) || defined(NDEBUG))
319 // On the Mac, all stderr output goes to the Console log and causes clutter.
320 // So in opt builds, don't log to stderr unless the user specifically sets
321 // a preference to do so.
322 CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault,
323 "logToStdErr",
324 kCFStringEncodingUTF8);
325 CFStringRef domain = CFBundleGetIdentifier(CFBundleGetMainBundle());
326 if (key != NULL && domain != NULL) {
327 Boolean exists_and_is_valid;
328 Boolean should_log =
329 CFPreferencesGetAppBooleanValue(key, domain, &exists_and_is_valid);
330 // If the key doesn't exist or is invalid or is false, we will not log to
331 // stderr.
332 log_to_stderr = exists_and_is_valid && should_log;
333 }
334 if (key != NULL) {
335 CFRelease(key);
336 }
337#endif
338#if defined(WEBRTC_WIN)
339 // Always log to the debugger.
340 // Perhaps stderr should be controlled by a preference, as on Mac?
341 OutputDebugStringA(str.c_str());
342 if (log_to_stderr) {
343 // This handles dynamically allocated consoles, too.
344 if (HANDLE error_handle = ::GetStdHandle(STD_ERROR_HANDLE)) {
345 log_to_stderr = false;
346 DWORD written = 0;
347 ::WriteFile(error_handle, str.data(), static_cast<DWORD>(str.size()),
348 &written, 0);
349 }
350 }
Tommi0eefb4d2015-05-23 09:54:07 +0200351#endif // WEBRTC_WIN
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000352#if defined(WEBRTC_ANDROID)
353 // Android's logging facility uses severity to log messages but we
354 // need to map libjingle's severity levels to Android ones first.
355 // Also write to stderr which maybe available to executable started
356 // from the shell.
357 int prio;
358 switch (severity) {
359 case LS_SENSITIVE:
360 __android_log_write(ANDROID_LOG_INFO, kLibjingle, "SENSITIVE");
361 if (log_to_stderr) {
362 fprintf(stderr, "SENSITIVE");
363 fflush(stderr);
364 }
365 return;
366 case LS_VERBOSE:
367 prio = ANDROID_LOG_VERBOSE;
368 break;
369 case LS_INFO:
370 prio = ANDROID_LOG_INFO;
371 break;
372 case LS_WARNING:
373 prio = ANDROID_LOG_WARN;
374 break;
375 case LS_ERROR:
376 prio = ANDROID_LOG_ERROR;
377 break;
378 default:
379 prio = ANDROID_LOG_UNKNOWN;
380 }
381
382 int size = str.size();
383 int line = 0;
384 int idx = 0;
385 const int max_lines = size / kMaxLogLineSize + 1;
386 if (max_lines == 1) {
387 __android_log_print(prio, kLibjingle, "%.*s", size, str.c_str());
388 } else {
389 while (size > 0) {
390 const int len = std::min(size, kMaxLogLineSize);
391 // Use the size of the string in the format (str may have \0 in the
392 // middle).
393 __android_log_print(prio, kLibjingle, "[%d/%d] %.*s",
394 line + 1, max_lines,
395 len, str.c_str() + idx);
396 idx += len;
397 size -= len;
398 ++line;
399 }
400 }
401#endif // WEBRTC_ANDROID
402 if (log_to_stderr) {
403 fprintf(stderr, "%s", str.c_str());
404 fflush(stderr);
405 }
406}
407
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000408//////////////////////////////////////////////////////////////////////
409// Logging Helpers
410//////////////////////////////////////////////////////////////////////
411
412void LogMultiline(LoggingSeverity level, const char* label, bool input,
413 const void* data, size_t len, bool hex_mode,
414 LogMultilineState* state) {
415 if (!LOG_CHECK_LEVEL_V(level))
416 return;
417
418 const char * direction = (input ? " << " : " >> ");
419
420 // NULL data means to flush our count of unprintable characters.
421 if (!data) {
422 if (state && state->unprintable_count_[input]) {
423 LOG_V(level) << label << direction << "## "
424 << state->unprintable_count_[input]
425 << " consecutive unprintable ##";
426 state->unprintable_count_[input] = 0;
427 }
428 return;
429 }
430
431 // The ctype classification functions want unsigned chars.
432 const unsigned char* udata = static_cast<const unsigned char*>(data);
433
434 if (hex_mode) {
435 const size_t LINE_SIZE = 24;
436 char hex_line[LINE_SIZE * 9 / 4 + 2], asc_line[LINE_SIZE + 1];
437 while (len > 0) {
438 memset(asc_line, ' ', sizeof(asc_line));
439 memset(hex_line, ' ', sizeof(hex_line));
andresp@webrtc.orgff689be2015-02-12 11:54:26 +0000440 size_t line_len = std::min(len, LINE_SIZE);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000441 for (size_t i = 0; i < line_len; ++i) {
442 unsigned char ch = udata[i];
443 asc_line[i] = isprint(ch) ? ch : '.';
444 hex_line[i*2 + i/4] = hex_encode(ch >> 4);
445 hex_line[i*2 + i/4 + 1] = hex_encode(ch & 0xf);
446 }
447 asc_line[sizeof(asc_line)-1] = 0;
448 hex_line[sizeof(hex_line)-1] = 0;
449 LOG_V(level) << label << direction
450 << asc_line << " " << hex_line << " ";
451 udata += line_len;
452 len -= line_len;
453 }
454 return;
455 }
456
457 size_t consecutive_unprintable = state ? state->unprintable_count_[input] : 0;
458
459 const unsigned char* end = udata + len;
460 while (udata < end) {
461 const unsigned char* line = udata;
462 const unsigned char* end_of_line = strchrn<unsigned char>(udata,
463 end - udata,
464 '\n');
465 if (!end_of_line) {
466 udata = end_of_line = end;
467 } else {
468 udata = end_of_line + 1;
469 }
470
471 bool is_printable = true;
472
473 // If we are in unprintable mode, we need to see a line of at least
474 // kMinPrintableLine characters before we'll switch back.
475 const ptrdiff_t kMinPrintableLine = 4;
476 if (consecutive_unprintable && ((end_of_line - line) < kMinPrintableLine)) {
477 is_printable = false;
478 } else {
479 // Determine if the line contains only whitespace and printable
480 // characters.
481 bool is_entirely_whitespace = true;
482 for (const unsigned char* pos = line; pos < end_of_line; ++pos) {
483 if (isspace(*pos))
484 continue;
485 is_entirely_whitespace = false;
486 if (!isprint(*pos)) {
487 is_printable = false;
488 break;
489 }
490 }
491 // Treat an empty line following unprintable data as unprintable.
492 if (consecutive_unprintable && is_entirely_whitespace) {
493 is_printable = false;
494 }
495 }
496 if (!is_printable) {
497 consecutive_unprintable += (udata - line);
498 continue;
499 }
500 // Print out the current line, but prefix with a count of prior unprintable
501 // characters.
502 if (consecutive_unprintable) {
503 LOG_V(level) << label << direction << "## " << consecutive_unprintable
504 << " consecutive unprintable ##";
505 consecutive_unprintable = 0;
506 }
507 // Strip off trailing whitespace.
508 while ((end_of_line > line) && isspace(*(end_of_line-1))) {
509 --end_of_line;
510 }
511 // Filter out any private data
512 std::string substr(reinterpret_cast<const char*>(line), end_of_line - line);
513 std::string::size_type pos_private = substr.find("Email");
514 if (pos_private == std::string::npos) {
515 pos_private = substr.find("Passwd");
516 }
517 if (pos_private == std::string::npos) {
518 LOG_V(level) << label << direction << substr;
519 } else {
520 LOG_V(level) << label << direction << "## omitted for privacy ##";
521 }
522 }
523
524 if (state) {
525 state->unprintable_count_[input] = consecutive_unprintable;
526 }
527}
528
529//////////////////////////////////////////////////////////////////////
530
531} // namespace rtc