Skip to content

Commit 980dd7f

Browse files
committed
Separate stdout and stderr in dlog output
1 parent abaa950 commit 980dd7f

File tree

4 files changed

+70
-58
lines changed

4 files changed

+70
-58
lines changed

shell/platform/tizen/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ source_set("flutter_tizen") {
5050
"key_event_handler.cc",
5151
"tizen_embedder_engine.cc",
5252
"tizen_event_loop.cc",
53+
"tizen_log.cc",
5354
"tizen_renderer.cc",
5455
"tizen_vsync_waiter.cc",
5556
"touch_event_handler.cc",

shell/platform/tizen/flutter_tizen.cc

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
#include "public/flutter_tizen.h"
77

8-
#include <inttypes.h>
9-
#include <unistd.h>
10-
118
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h"
129
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_message_codec.h"
1310
#include "flutter/shell/platform/common/cpp/incoming_message_dispatcher.h"
@@ -21,62 +18,10 @@ struct FlutterWindowControllerState {
2118
std::unique_ptr<TizenEmbedderEngine> engine;
2219
};
2320

24-
// The pipe used for logging to dlog.
25-
static int logging_pipe[2];
26-
// The thread that constantly writes out stdout to dlog through the pipe.
27-
// Only one logging thread should exist per process.
28-
static pthread_t logging_thread;
29-
30-
static void* LoggingFunction(void*) {
31-
ssize_t size;
32-
char buffer[1024];
33-
34-
while ((size = read(logging_pipe[0], buffer, sizeof(buffer) - 1)) > 0) {
35-
buffer[size] = 0;
36-
__dlog_print(LOG_ID_MAIN, DLOG_INFO, LOG_TAG, "%s", buffer);
37-
}
38-
39-
close(logging_pipe[0]);
40-
close(logging_pipe[1]);
41-
42-
return nullptr;
43-
}
44-
45-
// Redirects the process's standard output/error to dlog for use by the flutter
46-
// tools.
47-
bool InitializeLogging() {
48-
if (logging_thread) {
49-
FT_LOGD("The logging thread already exists.");
50-
return true;
51-
}
52-
53-
if (pipe(logging_pipe) < 0) {
54-
FT_LOGE("Failed to create a pipe.");
55-
return false;
56-
}
57-
58-
if (dup2(logging_pipe[1], 1) < 0 || dup2(logging_pipe[1], 2) < 0) {
59-
FT_LOGE("Failed to duplicate file descriptors.");
60-
return false;
61-
}
62-
63-
if (pthread_create(&logging_thread, 0, LoggingFunction, 0) != 0) {
64-
FT_LOGE("Failed to create a logging thread.");
65-
logging_thread = 0;
66-
return false;
67-
}
68-
69-
if (pthread_detach(logging_thread) != 0) {
70-
FT_LOGE("Failed to detach the logging thread.");
71-
return false;
72-
}
73-
return true;
74-
}
75-
7621
FlutterWindowControllerRef FlutterCreateWindow(
7722
const FlutterWindowProperties& window_properties,
7823
const FlutterEngineProperties& engine_properties) {
79-
InitializeLogging();
24+
StartLogging();
8025

8126
auto state = std::make_unique<FlutterWindowControllerState>();
8227
state->engine = std::make_unique<TizenEmbedderEngine>(window_properties);
@@ -221,6 +166,7 @@ void FlutterNotifyLowMemoryWarning(FlutterWindowControllerRef controller) {
221166

222167
void FlutterRotateWindow(FlutterWindowControllerRef controller,
223168
int32_t degree) {
169+
FT_LOGW("Deprecated API. Use SystemChrome.setPreferredOrientations instead.");
224170
}
225171

226172
int64_t FlutterRegisterExternalTexture(

shell/platform/tizen/tizen_log.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. 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 "tizen_log.h"
6+
7+
#include <pthread.h>
8+
#include <unistd.h>
9+
10+
static int stdout_pipe[2];
11+
static int stderr_pipe[2];
12+
static pthread_t stdout_thread;
13+
static pthread_t stderr_thread;
14+
static bool is_running = false;
15+
16+
static void* LoggingFunction(void* arg) {
17+
int* pipe = (int*)arg;
18+
int priority = pipe == stdout_pipe ? DLOG_INFO : DLOG_ERROR;
19+
20+
ssize_t size;
21+
char buffer[1024];
22+
23+
while ((size = read(pipe[0], buffer, sizeof(buffer) - 1)) > 0) {
24+
buffer[size] = 0;
25+
__dlog_print(LOG_ID_MAIN, priority, LOG_TAG, "%s", buffer);
26+
}
27+
28+
close(pipe[0]);
29+
close(pipe[1]);
30+
31+
return nullptr;
32+
}
33+
34+
void StartLogging() {
35+
if (is_running) {
36+
FT_LOGD("The threads are already running.");
37+
return;
38+
}
39+
40+
if (pipe(stdout_pipe) < 0 || pipe(stderr_pipe) < 0) {
41+
FT_LOGE("Failed to create pipes.");
42+
return;
43+
}
44+
45+
if (dup2(stdout_pipe[1], 1) < 0 || dup2(stderr_pipe[1], 2) < 0) {
46+
FT_LOGE("Failed to duplicate file descriptors.");
47+
return;
48+
}
49+
50+
if (pthread_create(&stdout_thread, 0, LoggingFunction, stdout_pipe) != 0 ||
51+
pthread_create(&stderr_thread, 0, LoggingFunction, stderr_pipe) != 0) {
52+
FT_LOGE("Failed to create threads.");
53+
return;
54+
}
55+
56+
if (pthread_detach(stdout_thread) != 0 ||
57+
pthread_detach(stderr_thread) != 0) {
58+
FT_LOGW("Failed to detach threads.");
59+
}
60+
is_running = true;
61+
}

shell/platform/tizen/tizen_log.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44

55
#ifndef EMBEDDER_TIZEN_LOG_H_
66
#define EMBEDDER_TIZEN_LOG_H_
7+
78
#include <dlog.h>
89

910
#include <cassert>
1011
#include <cstdlib>
11-
#include <string>
12+
13+
// Start logging threads which constantly redirect stdout/stderr to dlog.
14+
// The threads can be started only once per process.
15+
void StartLogging();
1216

1317
#ifdef LOG_TAG
1418
#undef LOG_TAG
@@ -47,7 +51,7 @@
4751
FT_LOGE("RELEASE_ASSERT"); \
4852
abort(); \
4953
} \
50-
} while (0);
54+
} while (0)
5155

5256
#define FT_RELEASE_ASSERT_NOT_REACHED() \
5357
do { \

0 commit comments

Comments
 (0)