1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | #include "tonic/common/log.h" |
6 | |
7 | #include <cstdarg> |
8 | #include <cstdio> |
9 | #include <memory> |
10 | |
11 | namespace tonic { |
12 | |
13 | namespace { |
14 | |
15 | std::function<void(const char*)> log_handler; |
16 | |
17 | } // namespace |
18 | |
19 | void Log(const char* format, ...) { |
20 | va_list ap; |
21 | va_start(ap, format); |
22 | int result = vsnprintf(nullptr, 0, format, ap); |
23 | va_end(ap); |
24 | |
25 | if (result < 0) { |
26 | return; |
27 | } |
28 | |
29 | int size = result + 1; |
30 | std::unique_ptr<char[]> message(new char[size]); |
31 | va_start(ap, format); |
32 | result = vsnprintf(message.get(), size, format, ap); |
33 | va_end(ap); |
34 | |
35 | if (result < 0) { |
36 | return; |
37 | } |
38 | |
39 | if (log_handler) { |
40 | log_handler(message.get()); |
41 | } else { |
42 | printf("%s\n" , message.get()); |
43 | } |
44 | } |
45 | |
46 | void SetLogHandler(std::function<void(const char*)> handler) { |
47 | log_handler = handler; |
48 | } |
49 | |
50 | } // namespace tonic |
51 | |