split unity_console.h to unity_console.cc and unity_console.h and make the details of native console logging invisible to users

This commit is contained in:
guanghuispark 2020-12-10 10:48:10 +08:00
Родитель 9b817e3e0d
Коммит 819407eaa9
6 изменённых файлов: 46 добавлений и 22 удалений

Просмотреть файл

@ -79,7 +79,7 @@ namespace Unity.UIWidgets.engine2 {
_width, _height, _devicePixelRatio, Application.streamingAssetsPath);
Input_OnEnable();
NativeConsole.InitNativeConsoleDelegate(NativeConsole.LogMessageFromCpp);
NativeConsole.OnEnable();
}
protected virtual void main() {

Просмотреть файл

@ -5,14 +5,18 @@ using System.Runtime.InteropServices;
using NativeBindings = Unity.UIWidgets.ui.NativeBindings;
public static class NativeConsole {
public delegate void LogDelegate(IntPtr message, int iSize);
internal delegate void LogDelegate(IntPtr message, int iSize);
[DllImport(NativeBindings.dllName)]
public static extern void InitNativeConsoleDelegate(LogDelegate log);
internal static extern void InitNativeConsoleDelegate(LogDelegate log);
[MonoPInvokeCallback(typeof(LogDelegate))]
public static void LogMessageFromCpp(IntPtr message, int iSize) {
internal static void LogMessageFromCpp(IntPtr message, int iSize) {
Debug.Log(Marshal.PtrToStringAnsi(message, iSize));
}
public static void OnEnable()
{
InitNativeConsoleDelegate(LogMessageFromCpp);
}
}

Просмотреть файл

@ -308,6 +308,8 @@ class Build
"src/shell/platform/unity/unity_surface_manager.h",
"src/shell/platform/unity/win32_task_runner.cc",
"src/shell/platform/unity/win32_task_runner.h",
"src/shell/platform/unity/unity_console.cc",
"src/shell/platform/unity/unity_console.h",
"src/shell/version/version.cc",
"src/shell/version/version.h",

Просмотреть файл

@ -11,7 +11,6 @@
#include "shell/common/switches.h"
#include "uiwidgets_system.h"
#include "unity_external_texture_gl.h"
#include "unity_console.h"
namespace uiwidgets {
@ -459,13 +458,11 @@ UIWIDGETS_API(void)
UIWidgetsPanel_onMouseDown(UIWidgetsPanel* panel, float x, float y,
int button) {
panel->OnMouseDown(x, y, button);
UnityConsole::WriteLine("OnMouseDown");
}
UIWIDGETS_API(void)
UIWidgetsPanel_onMouseUp(UIWidgetsPanel* panel, float x, float y, int button) {
panel->OnMouseUp(x, y, button);
UnityConsole::WriteLine("OnMouseUp");
}
UIWIDGETS_API(void)

Просмотреть файл

@ -0,0 +1,22 @@
#include "unity_console.h"
#include <stdarg.h>
namespace uiwidgets {
void UnityConsole::WriteLine(const char* fmt, ...) {
char log_str[512] = { 0 };
va_list ap;
va_start(ap, fmt);
vsprintf(log_str, fmt, ap);
_log(log_str, strlen(log_str));
va_end(ap);
}
LogDelegate UnityConsole::_log;
UIWIDGETS_API(void)
InitNativeConsoleDelegate(LogDelegate Log) {
UnityConsole::_log = Log;
}
}

Просмотреть файл

@ -1,27 +1,26 @@
#pragma once
#include "runtime/mono_api.h"
namespace uiwidgets {
typedef void (*LogDelegate)(char* message, int iSize);
extern "C"{
class UnityConsole{
public:
static void (*Log)(char* message,int iSize);
/*
static LogDelegate _log;
/**
output the log to unity editor console window
@param fmt log format
@param ... log args
@return null
example:
UnityConsole::WriteLine("output log without fmt param");
UnityConsole::WriteLine("%s: %d + %d = %d","output log with param", 1, 2, 3);
*/
static void WriteLine(const char* fmt, ...){
char log_str[512] = { 0 };
va_list ap;
va_start(ap, fmt);
sprintf_s(log_str, fmt, ap);
Log(log_str, strlen(log_str));
va_end(ap);
}
static void WriteLine(const char* fmt, ...);
};
void (*UnityConsole::Log)(char* message, int iSize);
UIWIDGETS_API(void)
InitNativeConsoleDelegate(LogDelegate Log){ UnityConsole::Log = Log; }
}
} // namespace uiwidgets