Commit 62efcea2 authored by Ghislain MARY's avatar Ghislain MARY

Configurable logger.

parent abf85311
......@@ -9,30 +9,45 @@ using namespace Linphone::Core;
Linphone::Core::BackgroundModeLogger::BackgroundModeLogger() :
stream(nullptr)
stream(nullptr), enabled(false), dest(OutputTraceDest::File), filename(nullptr)
{
}
Linphone::Core::BackgroundModeLogger::~BackgroundModeLogger()
{
if (stream) {
}
void Linphone::Core::BackgroundModeLogger::Configure(bool enable, OutputTraceDest dest, Platform::String^ filename)
{
std::lock_guard<std::recursive_mutex> lock(this->lock);
this->enabled = enable;
this->dest = dest;
if ((dest == OutputTraceDest::File) && (filename != this->filename) && (this->stream != nullptr)) {
stream->close();
delete stream;
stream = nullptr;
}
this->filename = filename;
}
void Linphone::Core::BackgroundModeLogger::OutputTrace(OutputTraceLevel level, Platform::String^ msg)
{
std::lock_guard<std::recursive_mutex> lock(lock);
if (!stream) {
StorageFolder^ localFolder = ApplicationData::Current->LocalFolder;
task<StorageFile^>(localFolder->CreateFileAsync("Linphone.log", CreationCollisionOption::ReplaceExisting)).then([this](StorageFile^ file) {
stream = new std::ofstream(file->Path->Data());
}).wait();
}
if (stream) {
*stream << Utils::pstoccs(msg);
stream->flush();
if (this->enabled) {
std::lock_guard<std::recursive_mutex> lock(this->lock);
if (this->dest == OutputTraceDest::Debugger) {
OutputDebugString(msg->Data());
}
else if (this->dest == OutputTraceDest::File) {
if (this->stream == nullptr) {
StorageFolder^ localFolder = ApplicationData::Current->LocalFolder;
task<StorageFile^>(localFolder->CreateFileAsync(this->filename, CreationCollisionOption::ReplaceExisting)).then([this](StorageFile^ file) {
this->stream = new std::ofstream(file->Path->Data());
}).wait();
}
if (this->stream != nullptr) {
*(this->stream) << Utils::pstoccs(msg);
this->stream->flush();
}
}
}
}
......@@ -15,6 +15,7 @@ namespace Linphone
{
public:
virtual void OutputTrace(OutputTraceLevel level, Platform::String^ msg);
void Configure(bool enable, OutputTraceDest dest, Platform::String^ filename);
private:
friend ref class Linphone::Core::Globals;
......@@ -24,6 +25,9 @@ namespace Linphone
std::ofstream *stream;
std::recursive_mutex lock;
bool enabled;
OutputTraceDest dest;
Platform::String^ filename;
};
}
}
\ No newline at end of file
......@@ -119,5 +119,14 @@ namespace Linphone
Error = 3,
Raw = 4
};
/// <summary>
/// The output destinations for logging
/// </summary>
public enum class OutputTraceDest : int
{
Debugger = 0,
File = 1
};
}
}
\ No newline at end of file
......@@ -10,12 +10,9 @@ using namespace Platform;
#define MAX_TRACE_SIZE 2048
#define MAX_SUITE_NAME_SIZE 128
static bool sDebugEnabled = false;
static OutputTraceListener^ sTraceListener;
static void nativeOutputTraceHandler(OutputTraceLevel lev, const char *fmt, va_list args)
{
if (sDebugEnabled) {
if (Globals::Instance->LinphoneCoreFactory->OutputTraceListener) {
wchar_t wstr[MAX_TRACE_SIZE];
std::string str;
str.resize(MAX_TRACE_SIZE);
......@@ -23,9 +20,7 @@ static void nativeOutputTraceHandler(OutputTraceLevel lev, const char *fmt, va_l
if (len >= MAX_TRACE_SIZE) ((char *)str.c_str())[MAX_TRACE_SIZE - 1] = '\0';
mbstowcs(wstr, str.c_str(), sizeof(wstr));
String^ msg = ref new String(wstr);
if (sTraceListener) {
sTraceListener->OutputTrace(lev, msg);
}
Globals::Instance->LinphoneCoreFactory->OutputTraceListener->OutputTrace(lev, msg);
}
}
......@@ -43,15 +38,6 @@ static void LinphoneNativeOutputTraceHandler(OrtpLogLevel lev, const char *fmt,
nativeOutputTraceHandler(level, fmt2, args);
}
void LinphoneCoreFactory::SetDebugMode(Platform::Boolean enable, OutputTraceListener^ traceListener)
{
if (enable) {
sTraceListener = traceListener;
} else {
sTraceListener = nullptr;
}
sDebugEnabled = enable;
}
void LinphoneCoreFactory::CreateLinphoneCore(Linphone::Core::LinphoneCoreListener^ listener, Platform::String^ userConfig, Platform::String^ factoryConfig, Platform::Object^ userData)
{
......@@ -86,6 +72,16 @@ Linphone::Core::LinphoneCore^ LinphoneCoreFactory::LinphoneCore::get()
return this->linphoneCore;
}
Linphone::Core::OutputTraceListener^ LinphoneCoreFactory::OutputTraceListener::get()
{
return this->outputTraceListener;
}
void LinphoneCoreFactory::OutputTraceListener::set(Linphone::Core::OutputTraceListener^ listener)
{
this->outputTraceListener = listener;
}
LinphoneCoreFactory::LinphoneCoreFactory() :
linphoneCore(nullptr)
{
......
......@@ -26,8 +26,12 @@ namespace Linphone
Linphone::Core::LinphoneCore^ get();
}
void SetDebugMode(Platform::Boolean enable, OutputTraceListener^ traceListener);
property OutputTraceListener^ OutputTraceListener
{
Linphone::Core::OutputTraceListener^ get();
void set(Linphone::Core::OutputTraceListener^ listener);
}
void CreateLinphoneCore(LinphoneCoreListener^ listener, Platform::String^ userConfig, Platform::String^ factoryConfig, Platform::Object^ userData);
void CreateLinphoneCore(LinphoneCoreListener^ listener);
......@@ -45,6 +49,7 @@ namespace Linphone
friend ref class Linphone::Core::Globals;
Linphone::Core::LinphoneCore^ linphoneCore;
Linphone::Core::OutputTraceListener^ outputTraceListener;
LinphoneCoreFactory();
~LinphoneCoreFactory();
......
......@@ -205,16 +205,20 @@ namespace Linphone.Model
/// </summary>
public void InitLinphoneCore()
{
server.LinphoneCoreFactory.SetDebugMode(SettingsManager.isDebugEnabled, server.BackgroundModeLogger);
Logger.Instance.TraceListener = server.BackgroundModeLogger;
if (server.LinphoneCore != null)
if ((server.LinphoneCoreFactory != null) && (server.LinphoneCore != null))
{
// Reconnect the listeners when coming back from background mode
server.LinphoneCore.CoreListener = this;
return;
}
// To have the debug output in the debugger use the following commented configure and set your debugger to native mode
//server.BackgroundModeLogger.Configure(SettingsManager.isDebugEnabled, OutputTraceDest.Debugger, "");
// Else output the debug traces to a file
server.BackgroundModeLogger.Configure(SettingsManager.isDebugEnabled, OutputTraceDest.File, "Linphone.log");
server.LinphoneCoreFactory.OutputTraceListener = server.BackgroundModeLogger;
Logger.Instance.TraceListener = server.BackgroundModeLogger;
server.LinphoneCoreFactory.CreateLinphoneCore(this);
InitProxyConfig();
Logger.Msg("[LinphoneManager] LinphoneCore created");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment