improve log.

This commit is contained in:
hecomi
2016-11-13 21:59:22 +09:00
parent 3567182a4d
commit c83ce50468
9 changed files with 142 additions and 79 deletions
@@ -37,6 +37,7 @@ public enum MonitorState
CurrentlyNotAvailable = 4,
SessionDisconnected = 5,
AccessLost = 6,
Unknown = 999,
}
public enum DebugMode
@@ -12,22 +12,25 @@ public class Monitor
switch (state)
{
case MonitorState.Available:
break;
case MonitorState.InvalidArg:
Debug.LogErrorFormat("[uDD] {0}:{1} => Invalid.", id, name);
break;
case MonitorState.AccessDenied:
Debug.LogErrorFormat("[uDD] {0}:{1} => Access Denied.", id, name);
Debug.LogWarningFormat("[uDD] {0}:{1} => Access Denied.", id, name);
break;
case MonitorState.Unsupported:
Debug.LogErrorFormat("[uDD] {0}:{1} => Unsupported.", id, name);
Debug.LogWarningFormat("[uDD] {0}:{1} => Unsupported.", id, name);
break;
case MonitorState.SessionDisconnected:
Debug.LogErrorFormat("[uDD] {0}:{1} => Disconnected.", id, name);
Debug.LogWarningFormat("[uDD] {0}:{1} => Disconnected.", id, name);
break;
case MonitorState.NotSet:
Debug.LogErrorFormat("[uDD] {0}:{1} => Something wrong.", id, name);
break;
default:
Debug.LogErrorFormat("[uDD] {0}:{1} => Unknown error.", id, name);
break;
}
@@ -4,81 +4,28 @@
#include "Debug.h"
decltype(Debug::mode_) Debug::mode_ = Debug::Mode::kFile;
decltype(Debug::mode_) Debug::mode_ = Debug::Mode::File;
decltype(Debug::logFunc_) Debug::logFunc_ = nullptr;
decltype(Debug::errFunc_) Debug::errFunc_ = nullptr;
decltype(Debug::fs_) Debug::fs_;
decltype(Debug::ss_) Debug::ss_;
void Debug::Initialize()
{
if (mode_ == Mode::kFile)
if (mode_ == Mode::File)
{
fs_.open("uDesktopDuplication.log");
Debug::Log("Start");
}
}
void Debug::Finalize()
{
fs_.close();
}
void Debug::Log(const char* msg)
{
switch (mode_)
if (mode_ == Mode::File)
{
case Mode::kNone:
{
break;
}
case Mode::kFile:
{
if (fs_.good())
{
fs_ << "[uDD::Log] " << msg << std::endl;
}
break;
}
case Mode::kUnityLog:
{
if (logFunc_ == nullptr)
{
char buf[256];
sprintf_s(buf, 256, "[uDD::Log] %s", msg);
logFunc_(buf);
break;
}
}
}
}
void Debug::Error(const char* msg)
{
switch (mode_)
{
case Mode::kNone:
{
break;
}
case Mode::kFile:
{
if (fs_.good())
{
fs_ << "[uDD::Err] " << msg << std::endl;
}
break;
}
case Mode::kUnityLog:
{
if (logFunc_ == nullptr)
{
char buf[256];
sprintf_s(buf, 256, "[uDD::Err] %s", msg);
errFunc_(buf);
}
}
Debug::Log("Stop");
fs_.close();
}
}
@@ -1,5 +1,6 @@
#pragma once
#include <fstream>
#include <sstream>
#include "IUnityInterface.h"
// Logging
@@ -8,9 +9,9 @@ class Debug
public:
enum class Mode
{
kNone = 0,
kFile = 1,
kUnityLog = 2,
None = 0,
File = 1,
UnityLog = 2,
};
using DebugLogFuncPtr = void(UNITY_INTERFACE_API *)(const char*);
@@ -21,13 +22,89 @@ public:
static void SetLogFunc(DebugLogFuncPtr func) { logFunc_ = func; }
static void SetErrorFunc(DebugLogFuncPtr func) { errFunc_ = func; }
private:
enum class Level
{
Log,
Error
};
template <class T>
static void Output(T&& arg)
{
if (mode_ == Mode::None) return;
if (ss_.good())
{
ss_ << std::forward<T>(arg);
}
}
static void Flush(Level level)
{
switch (mode_)
{
case Mode::None:
{
return;
}
case Mode::File:
{
if (fs_.good() && ss_.good())
{
const auto str = ss_.str();
fs_ << str << std::endl;
fs_.flush();
}
break;
}
case Mode::UnityLog:
{
if (ss_.good())
{
switch (level)
{
case Level::Log : logFunc_(ss_.str().c_str()); break;
case Level::Error : errFunc_(ss_.str().c_str()); break;
}
}
break;
}
}
ss_.str("");
ss_.clear(std::stringstream::goodbit);
}
template <class Arg, class... RestArgs>
static void _Log(Level level, Arg&& arg, RestArgs&&... restArgs)
{
Output(std::forward<Arg>(arg));
_Log(level, std::forward<RestArgs>(restArgs)...);
}
static void _Log(Level level)
{
Flush(level);
}
public:
static void Log(const char* msg);
static void Error(const char* msg);
template <class Arg, class... RestArgs>
static void Log(Arg&& arg, RestArgs&&... restArgs)
{
Output("[uDD::Log] ");
_Log(Level::Log, std::forward<Arg>(arg), std::forward<RestArgs>(restArgs)...);
}
template <class Arg, class... RestArgs>
static void Error(Arg&& arg, RestArgs&&... restArgs)
{
Output("[uDD::Error] ");
_Log(Level::Error, std::forward<Arg>(arg), std::forward<RestArgs>(restArgs)...);
}
private:
static Mode mode_;
static std::ofstream fs_;
static std::ostringstream ss_;
static DebugLogFuncPtr logFunc_;
static DebugLogFuncPtr errFunc_;
};
@@ -38,6 +38,9 @@ void Monitor::Initialize(IDXGIOutput* output)
case S_OK:
state_ = State::Available;
Debug::Log("Monitor::Initialize() => OK.");
Debug::Log(" ID : ", GetId());
Debug::Log(" Size : (", GetWidth(), ", ", GetHeight(), ")");
Debug::Log(" DPI : (", GetDpiX(), ", ", GetDpiY(), ")");
break;
case E_INVALIDARG:
state_ = State::InvalidArg;
@@ -186,25 +189,25 @@ bool Monitor::IsPrimary() const
int Monitor::GetLeft() const
{
return outputDesc_.DesktopCoordinates.left;
return static_cast<int>(outputDesc_.DesktopCoordinates.left);
}
int Monitor::GetRight() const
{
return outputDesc_.DesktopCoordinates.right;
return static_cast<int>(outputDesc_.DesktopCoordinates.right);
}
int Monitor::GetTop() const
{
return outputDesc_.DesktopCoordinates.top;
return static_cast<int>(outputDesc_.DesktopCoordinates.top);
}
int Monitor::GetBottom() const
{
return outputDesc_.DesktopCoordinates.bottom;
return static_cast<int>(outputDesc_.DesktopCoordinates.bottom);
}
@@ -9,6 +9,7 @@
#include "IUnityGraphicsD3D11.h"
#include "Common.h"
#include "Debug.h"
#include "Monitor.h"
#include "Cursor.h"
#include "MonitorManager.h"
@@ -67,11 +68,36 @@ void MonitorManager::RequireReinitilization()
void MonitorManager::Reinitialize()
{
Debug::Log("Reinitialize()");
Initialize();
SendMessageToUnity(Message::Reinitialized);
}
void MonitorManager::CheckMonitorNumbers()
{
ComPtr<IDXGIFactory1> factory;
CreateDXGIFactory1(IID_PPV_ARGS(&factory));
int id = 0;
ComPtr<IDXGIAdapter1> adapter;
for (int i = 0; (factory->EnumAdapters1(i, &adapter) != DXGI_ERROR_NOT_FOUND); ++i)
{
ComPtr<IDXGIOutput> output;
for (int j = 0; (adapter->EnumOutputs(j, &output) != DXGI_ERROR_NOT_FOUND); ++j)
{
id++;
}
}
if (GetMonitorCount() != id)
{
Debug::Log("Monitor number changed: ", GetMonitorCount(), " => ", id);
RequireReinitilization();
}
}
std::shared_ptr<Monitor> MonitorManager::GetMonitor(int id) const
{
if (id >= 0 && id < monitors_.size())
@@ -84,6 +110,8 @@ std::shared_ptr<Monitor> MonitorManager::GetMonitor(int id) const
void MonitorManager::Update()
{
CheckMonitorNumbers();
if (isReinitializationRequired_)
{
Reinitialize();
@@ -16,6 +16,7 @@ public:
explicit MonitorManager();
~MonitorManager();
void Reinitialize();
void CheckMonitorNumbers();
void RequireReinitilization();
void SetCursorMonitorId(int id) { cursorMonitorId_ = id; }
int GetCursorMonitorId() const { return cursorMonitorId_; }
@@ -29,21 +29,24 @@ extern "C"
{
if (g_unity && !g_manager)
{
g_manager = std::make_unique<MonitorManager>();
Debug::Initialize();
g_manager = std::make_unique<MonitorManager>();
}
}
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API FinalizeUDD()
{
g_manager.reset();
if (g_manager)
{
g_manager.reset();
std::queue<Message> empty;
g_messages.swap(empty);
std::queue<Message> empty;
g_messages.swap(empty);
Debug::SetLogFunc(nullptr);
Debug::SetErrorFunc(nullptr);
Debug::Finalize();
Debug::SetLogFunc(nullptr);
Debug::SetErrorFunc(nullptr);
Debug::Finalize();
}
}
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces)