Skip to content

Commit

Permalink
Separate logging threads for stdout and stderr (#41)
Browse files Browse the repository at this point in the history
* Format files

* Separate stdout and stderr in dlog output
  • Loading branch information
swift-kim committed Dec 9, 2021
1 parent 43b242b commit 7ab30c2
Show file tree
Hide file tree
Showing 19 changed files with 98 additions and 74 deletions.
1 change: 1 addition & 0 deletions shell/platform/tizen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ source_set("flutter_tizen") {
"key_event_handler.cc",
"tizen_embedder_engine.cc",
"tizen_event_loop.cc",
"tizen_log.cc",
"tizen_renderer.cc",
"tizen_vsync_waiter.cc",
"touch_event_handler.cc",
Expand Down
1 change: 1 addition & 0 deletions shell/platform/tizen/channels/lifecycle_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ class LifecycleChannel {
private:
FLUTTER_API_SYMBOL(FlutterEngine) flutter_engine_;
};

#endif // EMBEDDER_LIFECYCLE_CHANNEL_H_
2 changes: 2 additions & 0 deletions shell/platform/tizen/channels/platform_view_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ std::string ExtractStringFromMap(const flutter::EncodableValue& arguments,
}
return std::string();
}

int ExtractIntFromMap(const flutter::EncodableValue& arguments,
const char* key) {
if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
Expand All @@ -33,6 +34,7 @@ int ExtractIntFromMap(const flutter::EncodableValue& arguments,
}
return -1;
}

double ExtractDoubleFromMap(const flutter::EncodableValue& arguments,
const char* key) {
if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
Expand Down
3 changes: 1 addition & 2 deletions shell/platform/tizen/channels/settings_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ static constexpr char PLATFORM_BRIGHTNESS[] = "platformBrightness";
SettingsChannel::SettingsChannel(flutter::BinaryMessenger* messenger)
: channel_(
std::make_unique<flutter::BasicMessageChannel<rapidjson::Document>>(
messenger,
CHANNEL_NAME,
messenger, CHANNEL_NAME,
&flutter::JsonMessageCodec::GetInstance())) {
system_settings_set_changed_cb(SYSTEM_SETTINGS_KEY_LOCALE_TIMEFORMAT_24HOUR,
OnSettingsChangedCallback, this);
Expand Down
2 changes: 2 additions & 0 deletions shell/platform/tizen/channels/settings_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define EMBEDDER_SETTINGS_CHANNEL_H_

#include <system/system_settings.h>

#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/basic_message_channel.h"
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/binary_messenger.h"
#include "flutter/shell/platform/common/cpp/json_message_codec.h"
Expand All @@ -22,4 +23,5 @@ class SettingsChannel {
void* user_data);
void SendSettingsEvent();
};

#endif // EMBEDDER_SETTINGS_CHANNEL_H_
2 changes: 0 additions & 2 deletions shell/platform/tizen/channels/text_input_channel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include "flutter/shell/platform/tizen/tizen_embedder_engine.h"
#include "flutter/shell/platform/tizen/tizen_log.h"
#include "stdlib.h"
#include "string.h"

static constexpr char kSetEditingStateMethod[] = "TextInput.setEditingState";
static constexpr char kClearClientMethod[] = "TextInput.clearClient";
Expand Down
7 changes: 4 additions & 3 deletions shell/platform/tizen/channels/text_input_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EMBEDDER_TEXT_INPUT_PLUGIN_H_
#define EMBEDDER_TEXT_INPUT_PLUGIN_H_
#ifndef EMBEDDER_TEXT_INPUT_CHANNEL_H_
#define EMBEDDER_TEXT_INPUT_CHANNEL_H_

#define EFL_BETA_API_SUPPORT
#include <Ecore_IMF.h>
Expand Down Expand Up @@ -82,4 +82,5 @@ class TextInputChannel {
TizenEmbedderEngine* engine_;
SoftwareKeyboardGeometry current_keyboard_geometry_;
};
#endif

#endif // EMBEDDER_TEXT_INPUT_CHANNEL_H_
58 changes: 2 additions & 56 deletions shell/platform/tizen/flutter_tizen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

#include "public/flutter_tizen.h"

#include <inttypes.h>
#include <unistd.h>

#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h"
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/standard_message_codec.h"
#include "flutter/shell/platform/common/cpp/incoming_message_dispatcher.h"
Expand All @@ -21,62 +18,10 @@ struct FlutterWindowControllerState {
std::unique_ptr<TizenEmbedderEngine> engine;
};

// The pipe used for logging to dlog.
static int logging_pipe[2];
// The thread that constantly writes out stdout to dlog through the pipe.
// Only one logging thread should exist per process.
static pthread_t logging_thread;

static void* LoggingFunction(void*) {
ssize_t size;
char buffer[1024];

while ((size = read(logging_pipe[0], buffer, sizeof(buffer) - 1)) > 0) {
buffer[size] = 0;
__dlog_print(LOG_ID_MAIN, DLOG_INFO, LOG_TAG, "%s", buffer);
}

close(logging_pipe[0]);
close(logging_pipe[1]);

return nullptr;
}

// Redirects the process's standard output/error to dlog for use by the flutter
// tools.
bool InitializeLogging() {
if (logging_thread) {
FT_LOGD("The logging thread already exists.");
return true;
}

if (pipe(logging_pipe) < 0) {
FT_LOGE("Failed to create a pipe.");
return false;
}

if (dup2(logging_pipe[1], 1) < 0 || dup2(logging_pipe[1], 2) < 0) {
FT_LOGE("Failed to duplicate file descriptors.");
return false;
}

if (pthread_create(&logging_thread, 0, LoggingFunction, 0) != 0) {
FT_LOGE("Failed to create a logging thread.");
logging_thread = 0;
return false;
}

if (pthread_detach(logging_thread) != 0) {
FT_LOGE("Failed to detach the logging thread.");
return false;
}
return true;
}

FlutterWindowControllerRef FlutterCreateWindow(
const FlutterWindowProperties& window_properties,
const FlutterEngineProperties& engine_properties) {
InitializeLogging();
StartLogging();

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

void FlutterRotateWindow(FlutterWindowControllerRef controller,
int32_t degree) {
FT_LOGW("Deprecated API. Use SystemChrome.setPreferredOrientations instead.");
}

int64_t FlutterRegisterExternalTexture(
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/tizen/public/flutter_platform_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ FLUTTER_EXPORT void FlutterRegisterViewFactory(
} // extern "C"
#endif

#endif // FLUTTER_SHELL_PLATFORM_TIZEN_PUBLIC_FLUTTER_PLATFORM_VIEW_H_
#endif // FLUTTER_SHELL_PLATFORM_TIZEN_PUBLIC_FLUTTER_PLATFORM_VIEW_H_
2 changes: 1 addition & 1 deletion shell/platform/tizen/public/flutter_texture_registrar.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ FLUTTER_EXPORT bool FlutterMarkExternalTextureFrameAvailable(
} // extern "C"
#endif

#endif // FLUTTER_SHELL_PLATFORM_TIZEN_PUBLIC_FLUTTER_TEXTURE_REGISTRAR_H_
#endif // FLUTTER_SHELL_PLATFORM_TIZEN_PUBLIC_FLUTTER_TEXTURE_REGISTRAR_H_
1 change: 1 addition & 0 deletions shell/platform/tizen/tizen_embedder_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,5 @@ class TizenEmbedderEngine : public TizenRenderer::Delegate {
// The current renderer transformation.
FlutterTransformation transformation_;
};

#endif // EMBEDDER_TIZEN_EMBEDDER_ENGINE_H_
61 changes: 61 additions & 0 deletions shell/platform/tizen/tizen_log.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "tizen_log.h"

#include <pthread.h>
#include <unistd.h>

static int stdout_pipe[2];
static int stderr_pipe[2];
static pthread_t stdout_thread;
static pthread_t stderr_thread;
static bool is_running = false;

static void* LoggingFunction(void* arg) {
int* pipe = (int*)arg;
int priority = pipe == stdout_pipe ? DLOG_INFO : DLOG_ERROR;

ssize_t size;
char buffer[1024];

while ((size = read(pipe[0], buffer, sizeof(buffer) - 1)) > 0) {
buffer[size] = 0;
__dlog_print(LOG_ID_MAIN, priority, LOG_TAG, "%s", buffer);
}

close(pipe[0]);
close(pipe[1]);

return nullptr;
}

void StartLogging() {
if (is_running) {
FT_LOGD("The threads are already running.");
return;
}

if (pipe(stdout_pipe) < 0 || pipe(stderr_pipe) < 0) {
FT_LOGE("Failed to create pipes.");
return;
}

if (dup2(stdout_pipe[1], 1) < 0 || dup2(stderr_pipe[1], 2) < 0) {
FT_LOGE("Failed to duplicate file descriptors.");
return;
}

if (pthread_create(&stdout_thread, 0, LoggingFunction, stdout_pipe) != 0 ||
pthread_create(&stderr_thread, 0, LoggingFunction, stderr_pipe) != 0) {
FT_LOGE("Failed to create threads.");
return;
}

if (pthread_detach(stdout_thread) != 0 ||
pthread_detach(stderr_thread) != 0) {
FT_LOGW("Failed to detach threads.");
}
is_running = true;
}
8 changes: 6 additions & 2 deletions shell/platform/tizen/tizen_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

#ifndef EMBEDDER_TIZEN_LOG_H_
#define EMBEDDER_TIZEN_LOG_H_

#include <dlog.h>

#include <cassert>
#include <cstdlib>
#include <string>

// Start logging threads which constantly redirect stdout/stderr to dlog.
// The threads can be started only once per process.
void StartLogging();

#ifdef LOG_TAG
#undef LOG_TAG
Expand Down Expand Up @@ -47,7 +51,7 @@
FT_LOGE("RELEASE_ASSERT"); \
abort(); \
} \
} while (0);
} while (0)

#define FT_RELEASE_ASSERT_NOT_REACHED() \
do { \
Expand Down
3 changes: 2 additions & 1 deletion shell/platform/tizen/tizen_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,5 @@ class TizenRenderer {
bool ChooseEGLConfiguration();
void PrintEGLError();
};
#endif //EMBEDDER_TIZEN_RENDERER_H

#endif // EMBEDDER_TIZEN_RENDERER_H
3 changes: 2 additions & 1 deletion shell/platform/tizen/tizen_renderer_ecore_wl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Eina_Bool TizenRendererEcoreWl::RotationEventCb(void *data, int type,
}

void TizenRendererEcoreWl::Show() { ecore_wl_window_show(ecore_wl_window_); }

void TizenRendererEcoreWl::SetRotate(int degree) {
ecore_wl_window_rotation_set(ecore_wl_window_, degree);
received_rotation = true;
Expand Down Expand Up @@ -129,4 +130,4 @@ TizenRenderer::TizenWindowGeometry TizenRendererEcoreWl::GetGeometry() {

int TizenRendererEcoreWl::GetEcoreWindowId() {
return ecore_wl_window_id_get(ecore_wl_window_);
}
}
7 changes: 5 additions & 2 deletions shell/platform/tizen/tizen_renderer_ecore_wl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#include <wayland-client.h>
#include <wayland-egl.h>

#include "tizen_renderer.h"
#define EFL_BETA_API_SUPPORT
#include <Ecore_Wayland.h>

#include "flutter/shell/platform/tizen/tizen_renderer.h"

class TizenRendererEcoreWl : public TizenRenderer {
public:
TizenRendererEcoreWl(TizenRenderer::Delegate& delegate, int32_t x, int32_t y,
Expand Down Expand Up @@ -40,4 +42,5 @@ class TizenRendererEcoreWl : public TizenRenderer {
wl_display* wl_display_ = nullptr;
static Eina_Bool RotationEventCb(void* data, int type, void* event);
};
#endif //EMBEDDER_TIZEN_RENDERER_ECORE_WL_H

#endif // EMBEDDER_TIZEN_RENDERER_ECORE_WL_H
1 change: 1 addition & 0 deletions shell/platform/tizen/tizen_renderer_ecore_wl2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ TizenRendererEcoreWl2::TizenRendererEcoreWl2(TizenRenderer::Delegate &delegate,
}

TizenRendererEcoreWl2::~TizenRendererEcoreWl2() { DestoryRenderer(); }

bool TizenRendererEcoreWl2::SetupDisplay() {
if (!ecore_wl2_init()) {
FT_LOGE("Could not initialize ecore_wl2");
Expand Down
7 changes: 4 additions & 3 deletions shell/platform/tizen/tizen_renderer_ecore_wl2.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
#ifndef EMBEDDER_TIZEN_RENDERER_ECORE_WL2_H
#define EMBEDDER_TIZEN_RENDERER_ECORE_WL2_H

#include "tizen_renderer.h"

#define EFL_BETA_API_SUPPORT
#include <Ecore_Wl2.h>

#include "flutter/shell/platform/tizen/tizen_renderer.h"

class TizenRendererEcoreWl2 : public TizenRenderer {
public:
TizenRendererEcoreWl2(TizenRenderer::Delegate &delegate, int32_t x, int32_t y,
Expand Down Expand Up @@ -39,4 +39,5 @@ class TizenRendererEcoreWl2 : public TizenRenderer {
Ecore_Wl2_Egl_Window *ecore_wl2_egl_window_ = nullptr;
static Eina_Bool RotationEventCb(void *data, int type, void *event);
};
#endif //EMBEDDER_TIZEN_RENDERER_ECORE_WL2_H

#endif // EMBEDDER_TIZEN_RENDERER_ECORE_WL2_H
1 change: 1 addition & 0 deletions shell/platform/tizen/touch_event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define EMBEDDER_TOUCH_EVENT_HANDLER_H_

#include <Ecore_Input.h>

#include <vector>

#include "flutter/shell/platform/embedder/embedder.h"
Expand Down

0 comments on commit 7ab30c2

Please sign in to comment.