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, CurrentlyNotAvailable = 4,
SessionDisconnected = 5, SessionDisconnected = 5,
AccessLost = 6, AccessLost = 6,
Unknown = 999,
} }
public enum DebugMode public enum DebugMode
@@ -12,22 +12,25 @@ public class Monitor
switch (state) switch (state)
{ {
case MonitorState.Available:
break;
case MonitorState.InvalidArg: case MonitorState.InvalidArg:
Debug.LogErrorFormat("[uDD] {0}:{1} => Invalid.", id, name); Debug.LogErrorFormat("[uDD] {0}:{1} => Invalid.", id, name);
break; break;
case MonitorState.AccessDenied: case MonitorState.AccessDenied:
Debug.LogErrorFormat("[uDD] {0}:{1} => Access Denied.", id, name); Debug.LogWarningFormat("[uDD] {0}:{1} => Access Denied.", id, name);
break; break;
case MonitorState.Unsupported: case MonitorState.Unsupported:
Debug.LogErrorFormat("[uDD] {0}:{1} => Unsupported.", id, name); Debug.LogWarningFormat("[uDD] {0}:{1} => Unsupported.", id, name);
break; break;
case MonitorState.SessionDisconnected: case MonitorState.SessionDisconnected:
Debug.LogErrorFormat("[uDD] {0}:{1} => Disconnected.", id, name); Debug.LogWarningFormat("[uDD] {0}:{1} => Disconnected.", id, name);
break; break;
case MonitorState.NotSet: case MonitorState.NotSet:
Debug.LogErrorFormat("[uDD] {0}:{1} => Something wrong.", id, name); Debug.LogErrorFormat("[uDD] {0}:{1} => Something wrong.", id, name);
break; break;
default: default:
Debug.LogErrorFormat("[uDD] {0}:{1} => Unknown error.", id, name);
break; break;
} }
@@ -4,81 +4,28 @@
#include "Debug.h" #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::logFunc_) Debug::logFunc_ = nullptr;
decltype(Debug::errFunc_) Debug::errFunc_ = nullptr; decltype(Debug::errFunc_) Debug::errFunc_ = nullptr;
decltype(Debug::fs_) Debug::fs_; decltype(Debug::fs_) Debug::fs_;
decltype(Debug::ss_) Debug::ss_;
void Debug::Initialize() void Debug::Initialize()
{ {
if (mode_ == Mode::kFile) if (mode_ == Mode::File)
{ {
fs_.open("uDesktopDuplication.log"); fs_.open("uDesktopDuplication.log");
Debug::Log("Start");
} }
} }
void Debug::Finalize() void Debug::Finalize()
{ {
fs_.close(); if (mode_ == Mode::File)
}
void Debug::Log(const char* msg)
{
switch (mode_)
{ {
case Mode::kNone: Debug::Log("Stop");
{ fs_.close();
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);
}
}
} }
} }
@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <fstream> #include <fstream>
#include <sstream>
#include "IUnityInterface.h" #include "IUnityInterface.h"
// Logging // Logging
@@ -8,9 +9,9 @@ class Debug
public: public:
enum class Mode enum class Mode
{ {
kNone = 0, None = 0,
kFile = 1, File = 1,
kUnityLog = 2, UnityLog = 2,
}; };
using DebugLogFuncPtr = void(UNITY_INTERFACE_API *)(const char*); using DebugLogFuncPtr = void(UNITY_INTERFACE_API *)(const char*);
@@ -21,13 +22,89 @@ public:
static void SetLogFunc(DebugLogFuncPtr func) { logFunc_ = func; } static void SetLogFunc(DebugLogFuncPtr func) { logFunc_ = func; }
static void SetErrorFunc(DebugLogFuncPtr func) { errFunc_ = 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: public:
static void Log(const char* msg); template <class Arg, class... RestArgs>
static void Error(const char* msg); 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: private:
static Mode mode_; static Mode mode_;
static std::ofstream fs_; static std::ofstream fs_;
static std::ostringstream ss_;
static DebugLogFuncPtr logFunc_; static DebugLogFuncPtr logFunc_;
static DebugLogFuncPtr errFunc_; static DebugLogFuncPtr errFunc_;
}; };
@@ -38,6 +38,9 @@ void Monitor::Initialize(IDXGIOutput* output)
case S_OK: case S_OK:
state_ = State::Available; state_ = State::Available;
Debug::Log("Monitor::Initialize() => OK."); Debug::Log("Monitor::Initialize() => OK.");
Debug::Log(" ID : ", GetId());
Debug::Log(" Size : (", GetWidth(), ", ", GetHeight(), ")");
Debug::Log(" DPI : (", GetDpiX(), ", ", GetDpiY(), ")");
break; break;
case E_INVALIDARG: case E_INVALIDARG:
state_ = State::InvalidArg; state_ = State::InvalidArg;
@@ -186,25 +189,25 @@ bool Monitor::IsPrimary() const
int Monitor::GetLeft() const int Monitor::GetLeft() const
{ {
return outputDesc_.DesktopCoordinates.left; return static_cast<int>(outputDesc_.DesktopCoordinates.left);
} }
int Monitor::GetRight() const int Monitor::GetRight() const
{ {
return outputDesc_.DesktopCoordinates.right; return static_cast<int>(outputDesc_.DesktopCoordinates.right);
} }
int Monitor::GetTop() const int Monitor::GetTop() const
{ {
return outputDesc_.DesktopCoordinates.top; return static_cast<int>(outputDesc_.DesktopCoordinates.top);
} }
int Monitor::GetBottom() const int Monitor::GetBottom() const
{ {
return outputDesc_.DesktopCoordinates.bottom; return static_cast<int>(outputDesc_.DesktopCoordinates.bottom);
} }
@@ -9,6 +9,7 @@
#include "IUnityGraphicsD3D11.h" #include "IUnityGraphicsD3D11.h"
#include "Common.h" #include "Common.h"
#include "Debug.h"
#include "Monitor.h" #include "Monitor.h"
#include "Cursor.h" #include "Cursor.h"
#include "MonitorManager.h" #include "MonitorManager.h"
@@ -67,11 +68,36 @@ void MonitorManager::RequireReinitilization()
void MonitorManager::Reinitialize() void MonitorManager::Reinitialize()
{ {
Debug::Log("Reinitialize()");
Initialize(); Initialize();
SendMessageToUnity(Message::Reinitialized); 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 std::shared_ptr<Monitor> MonitorManager::GetMonitor(int id) const
{ {
if (id >= 0 && id < monitors_.size()) if (id >= 0 && id < monitors_.size())
@@ -84,6 +110,8 @@ std::shared_ptr<Monitor> MonitorManager::GetMonitor(int id) const
void MonitorManager::Update() void MonitorManager::Update()
{ {
CheckMonitorNumbers();
if (isReinitializationRequired_) if (isReinitializationRequired_)
{ {
Reinitialize(); Reinitialize();
@@ -16,6 +16,7 @@ public:
explicit MonitorManager(); explicit MonitorManager();
~MonitorManager(); ~MonitorManager();
void Reinitialize(); void Reinitialize();
void CheckMonitorNumbers();
void RequireReinitilization(); void RequireReinitilization();
void SetCursorMonitorId(int id) { cursorMonitorId_ = id; } void SetCursorMonitorId(int id) { cursorMonitorId_ = id; }
int GetCursorMonitorId() const { return cursorMonitorId_; } int GetCursorMonitorId() const { return cursorMonitorId_; }
@@ -29,21 +29,24 @@ extern "C"
{ {
if (g_unity && !g_manager) if (g_unity && !g_manager)
{ {
g_manager = std::make_unique<MonitorManager>();
Debug::Initialize(); Debug::Initialize();
g_manager = std::make_unique<MonitorManager>();
} }
} }
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API FinalizeUDD() UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API FinalizeUDD()
{ {
g_manager.reset(); if (g_manager)
{
g_manager.reset();
std::queue<Message> empty; std::queue<Message> empty;
g_messages.swap(empty); g_messages.swap(empty);
Debug::SetLogFunc(nullptr); Debug::SetLogFunc(nullptr);
Debug::SetErrorFunc(nullptr); Debug::SetErrorFunc(nullptr);
Debug::Finalize(); Debug::Finalize();
}
} }
UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces) UNITY_INTERFACE_EXPORT void UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces)