Skip to content

Commit 6b1c226

Browse files
committed
Unify logging
1 parent b6c4147 commit 6b1c226

File tree

8 files changed

+260
-85
lines changed

8 files changed

+260
-85
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ set(SOURCES
3333
src/keys.cc
3434
src/egl_utils.cc
3535
src/utils.cc
36+
src/debug.cc
3637
src/wayland_display.cc
3738
src/elf.h
3839
src/macros.h
3940
src/keys.h
4041
src/utils.h
4142
src/egl_utils.h
43+
src/debug.h
4244
src/wayland_display.h
4345
)
4446

src/debug.cc

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// Copyright (c) 2021 Damian Wrobel <[email protected]>
2+
//
3+
// This software is provided 'as-is', without any express or implied
4+
// warranty. In no event will the authors be held liable for any damages
5+
// arising from the use of this software.
6+
//
7+
// Permission is granted to anyone to use this software for any purpose,
8+
// including commercial applications, and to alter it and redistribute it
9+
// freely, subject to the following restrictions:
10+
//
11+
// 1. The origin of this software must not be misrepresented; you must not
12+
// claim that you wrote the original software. If you use this software
13+
// in a product, an acknowledgment in the product documentation would
14+
// be appreciated but is not required.
15+
//.
16+
// 2. Altered source versions must be plainly marked as such, and must not
17+
// be misrepresented as being the original software.
18+
//
19+
// 3. This notice may not be removed or altered from any source
20+
// distribution.
21+
//
22+
23+
#include <array>
24+
#include <cstdarg>
25+
#include <cstdio>
26+
#include <cstring>
27+
#include <vector>
28+
#include <mutex>
29+
30+
#ifndef SYSLOG_NAMES
31+
#define SYSLOG_NAMES
32+
#endif
33+
#include <syslog.h>
34+
35+
#include "utils.h"
36+
#include "debug.h"
37+
38+
namespace flutter {
39+
40+
static int debug_level;
41+
42+
int syslog2level(const std::string &priority) {
43+
const char *c_name = priority.c_str();
44+
for (size_t i = 0; prioritynames[i].c_name != NULL; i++) {
45+
if (strstr(prioritynames[i].c_name, c_name)) {
46+
return prioritynames[i].c_val;
47+
}
48+
}
49+
50+
return LOG_INFO;
51+
}
52+
53+
void dbg_init() {
54+
debug_level = syslog2level(getEnv("FLUTTER_LAUNCHER_WAYLAND_DEBUG", std::string("info")));
55+
}
56+
57+
static const char *priority2prefix(const int priority) {
58+
static const struct {
59+
const int priority;
60+
const char *const prefix;
61+
} map[] = {
62+
{LOG_ERR, "ERROR: "},
63+
{LOG_WARNING, "WARNING: "},
64+
{LOG_INFO, "INFO: "},
65+
{LOG_DEBUG, "TRACE: "},
66+
};
67+
68+
for (size_t i = 0; i < std::size(map); i++) {
69+
if (priority == map[i].priority) {
70+
return map[i].prefix;
71+
}
72+
}
73+
74+
return "UNKNOWN: ";
75+
}
76+
77+
static void vdbg(const int priority, const char *format, va_list args1) {
78+
va_list args2;
79+
va_copy(args2, args1);
80+
81+
char buf[256];
82+
int rv = std::vsnprintf(buf, std::size(buf), format, args1);
83+
84+
if (rv < 0) {
85+
va_end(args2);
86+
return;
87+
}
88+
89+
if (static_cast<std::size_t>(rv) < std::size(buf)) {
90+
va_end(args2);
91+
92+
printf("%s%s", priority2prefix(priority), buf);
93+
fflush(stdout);
94+
} else {
95+
const size_t len = rv + 1;
96+
std::vector<char> str(len);
97+
std::vsnprintf(str.data(), len, format, args2);
98+
va_end(args2);
99+
100+
printf("%s%s", priority2prefix(priority), str.data());
101+
fflush(stdout);
102+
}
103+
}
104+
105+
// clang-format off
106+
#define dbg(type, priority) \
107+
void dbg##type(const char *format, ...) { \
108+
if (debug_level < (priority)) \
109+
return; \
110+
\
111+
va_list args; \
112+
va_start(args, format); \
113+
vdbg((priority), format, args); \
114+
va_end(args); \
115+
}
116+
117+
dbg(E, LOG_ERR)
118+
dbg(W, LOG_WARNING)
119+
dbg(I, LOG_INFO)
120+
dbg(T, LOG_DEBUG)
121+
// clang-format once
122+
123+
} // namespace flutter

src/debug.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2021 Damian Wrobel <[email protected]>
2+
//
3+
// This software is provided 'as-is', without any express or implied
4+
// warranty. In no event will the authors be held liable for any damages
5+
// arising from the use of this software.
6+
//
7+
// Permission is granted to anyone to use this software for any purpose,
8+
// including commercial applications, and to alter it and redistribute it
9+
// freely, subject to the following restrictions:
10+
//
11+
// 1. The origin of this software must not be misrepresented; you must not
12+
// claim that you wrote the original software. If you use this software
13+
// in a product, an acknowledgment in the product documentation would
14+
// be appreciated but is not required.
15+
//.
16+
// 2. Altered source versions must be plainly marked as such, and must not
17+
// be misrepresented as being the original software.
18+
//
19+
// 3. This notice may not be removed or altered from any source
20+
// distribution.
21+
//
22+
#pragma once
23+
24+
namespace flutter {
25+
26+
void dbg_init();
27+
28+
void dbgE(const char *format, ...) __attribute__((format(printf, 1, 2)));
29+
void dbgW(const char *format, ...) __attribute__((format(printf, 1, 2)));
30+
void dbgI(const char *format, ...) __attribute__((format(printf, 1, 2)));
31+
void dbgT(const char *format, ...) __attribute__((format(printf, 1, 2)));
32+
33+
} // namespace flutter

src/egl_utils.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <EGL/egl.h>
88
#include "egl_utils.h"
99
#include "utils.h"
10+
#include "debug.h"
1011

1112
namespace flutter {
1213

@@ -33,12 +34,12 @@ void LogLastEGLError() {
3334

3435
for (size_t i = 0; i < count; i++) {
3536
if (last_error == pairs[i].code) {
36-
FLWAY_ERROR << "EGL Error: " << pairs[i].name << " (" << pairs[i].code << ")" << std::endl;
37+
dbgE("EGL Error: %s (code: %d)\n", pairs[i].name, pairs[i].code);
3738
return;
3839
}
3940
}
4041

41-
FLWAY_ERROR << "Unknown EGL Error" << std::endl;
42+
dbgE("Unknown EGL Error (%d)\n", last_error);
4243
}
4344

4445
} // namespace flutter

src/macros.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,3 @@
1313
#define FLWAY_DISALLOW_COPY_AND_ASSIGN(TypeName) \
1414
FLWAY_DISALLOW_COPY(TypeName) \
1515
FLWAY_DISALLOW_ASSIGN(TypeName)
16-
17-
#define __FLWAY_LINE_PREFIX << __FILE__ << ":" << std::to_string(__LINE__) << ": "
18-
#define FLWAY_LOG std::cout << "LOG: " __FLWAY_LINE_PREFIX
19-
#define FLWAY_ERROR std::cerr << "ERROR: " __FLWAY_LINE_PREFIX
20-
#define FLWAY_WIP \
21-
std::cerr << "Work In Progress. Aborting." << std::endl; \
22-
abort();

src/main.cc

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <vector>
99

1010
#include "utils.h"
11+
#include "debug.h"
1112
#include "wayland_display.h"
1213

1314
static_assert(FLUTTER_ENGINE_VERSION == 1, "");
@@ -36,20 +37,46 @@ asset_bundle_path: The Flutter application code needs to be snapshotted using
3637
Flutter engine. To see all supported flags, run
3738
`flutter_tester --help` using the test binary included in the
3839
Flutter tools.
40+
41+
Supported environment variables:
42+
LANG=<string>
43+
Value encoded as per setlocale(3) (e.g. "szl_PL.utf8") is
44+
passed to the engine via UpdateLocales().
45+
46+
See also: https://man7.org/linux/man-pages/man3/setlocale.3.html
47+
48+
FLUTTER_WAYLAND_PIXEL_RATIO=<double>
49+
Overwrites the pixel aspect ratio reported
50+
to the engine by FlutterEngineSendWindowMetricsEvent().
51+
52+
See also: https://api.flutter.dev/flutter/dart-ui/Window/devicePixelRatio.html
53+
54+
FLUTTER_WAYLAND_MAIN_UI=<int>
55+
Non-zero value enables grabbing all keys (even without having
56+
a focus) using Xwayland keyboard grabbing protocol (assuming
57+
server implement this extension).
58+
59+
See also: https://github.com/wayland-project/wayland-protocols/tree/0a61d3516b10da4e65607a6dd97937ebedf6bcfa/unstable/xwayland-keyboard-grab
60+
61+
FLUTTER_LAUNCHER_WAYLAND_DEBUG=<string>
62+
where <string> can be any of syslog(3) prioritynames or its
63+
unique abbreviation e.g. "err", "warning", "info" or "debug".
3964
)~" << std::endl;
4065
}
4166

4267
static bool Main(std::vector<std::string> args) {
68+
dbg_init();
69+
4370
if (args.size() == 1) {
44-
std::cerr << " <Invalid Arguments> " << std::endl;
71+
dbgE("Invalid list of arguments\n");
4572
PrintUsage();
4673
return false;
4774
}
4875

4976
const auto asset_bundle_path = args[1];
5077

5178
if (!FlutterAssetBundleIsValid(asset_bundle_path)) {
52-
std::cerr << " <Invalid Flutter Asset Bundle> " << std::endl;
79+
dbgE("Invalid Flutter Asset Bundle\n");
5380
PrintUsage();
5481
return false;
5582
}
@@ -60,21 +87,16 @@ static bool Main(std::vector<std::string> args) {
6087
const std::vector<std::string> flutter_args(args.begin() + 1, args.end());
6188

6289
for (const auto &arg : flutter_args) {
63-
FLWAY_LOG << "Flutter arg: " << arg << std::endl;
90+
dbgI("Flutter arg: %s\n", arg.c_str());
6491
}
6592

6693
WaylandDisplay display(kWidth, kHeight, asset_bundle_path, flutter_args);
6794

6895
if (!display.IsValid()) {
69-
FLWAY_ERROR << "Wayland display was not valid." << std::endl;
96+
dbgI("Wayland display was not valid\n");
7097
return false;
7198
}
7299

73-
// if (!application.SetWindowSize(kWidth, kHeight)) {
74-
// FLWAY_ERROR << "Could not update Flutter application size." << std::endl;
75-
// return false;
76-
// }
77-
78100
return display.Run();
79101
}
80102

src/utils.cc

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <string.h>
99
#include <stdlib.h>
1010

11+
#include "debug.h"
1112
#include "utils.h"
1213

1314
namespace flutter {
@@ -47,18 +48,18 @@ std::string GetICUDataPath() {
4748

4849
do {
4950
if (std::ifstream(icu_data_path)) {
50-
std::cout << "Using: " << icu_data_path << std::endl;
51+
dbgI("Using %s\n", icu_data_path.c_str());
5152
break;
5253
}
5354

5455
icu_data_path = "/usr/share/flutter/icudtl.dat";
5556

5657
if (std::ifstream(icu_data_path)) {
57-
std::cout << "Using: " << icu_data_path << std::endl;
58+
dbgI("Using: %s\n", icu_data_path.c_str());
5859
break;
5960
}
6061

61-
FLWAY_ERROR << "Unnable to locate icudtl.dat file" << std::endl;
62+
dbgE("Unnable to locate icudtl.dat file\n");
6263
icu_data_path = "";
6364
} while (0);
6465

@@ -101,7 +102,7 @@ bool FileExistsAtPath(const std::string &path) {
101102

102103
bool FlutterAssetBundleIsValid(const std::string &bundle_path) {
103104
if (!FileExistsAtPath(bundle_path)) {
104-
FLWAY_ERROR << "Bundle directory: '" << bundle_path << "' does not exist." << std::endl;
105+
dbgE("Bundle directory: %s does not exist\n", bundle_path.c_str());
105106
return false;
106107
}
107108

@@ -111,7 +112,7 @@ bool FlutterAssetBundleIsValid(const std::string &bundle_path) {
111112
auto aotelf = FileExistsAtPath(aotelf_path);
112113

113114
if (!(kernel || aotelf)) {
114-
FLWAY_ERROR << "Could not found neither " << kernel_path << " nor " << aotelf_path << std::endl;
115+
dbgE("Could not found neither %s nor %s\n", kernel_path.c_str(), aotelf_path.c_str());
115116
return false;
116117
}
117118

0 commit comments

Comments
 (0)