Commit abf85311 authored by Ghislain MARY's avatar Ghislain MARY

Simplify logger.

parent a0f183ab
#include "BackgroundModeLogger.h"
#include "Utils.h"
#include <ppltasks.h>
using namespace concurrency;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
using namespace Linphone::Core;
Linphone::Core::BackgroundModeLogger::BackgroundModeLogger()
Linphone::Core::BackgroundModeLogger::BackgroundModeLogger() :
stream(nullptr)
{
}
Linphone::Core::BackgroundModeLogger::~BackgroundModeLogger()
{
if (stream) {
stream->close();
delete stream;
stream = nullptr;
}
}
void Linphone::Core::BackgroundModeLogger::OutputTrace(OutputTraceLevel level, Platform::String^ msg)
{
if (!dataWriter) {
std::lock_guard<std::recursive_mutex> lock(lock);
if (!stream) {
StorageFolder^ localFolder = ApplicationData::Current->LocalFolder;
task<StorageFile^>(localFolder->CreateFileAsync("Linphone.log", CreationCollisionOption::OpenIfExists)).then([this](StorageFile^ file) {
storageFile = file;
task<IRandomAccessStream^>(storageFile->OpenAsync(FileAccessMode::ReadWrite)).then([this](IRandomAccessStream^ stream) {
dataWriter = ref new DataWriter(stream);
});
task<StorageFile^>(localFolder->CreateFileAsync("Linphone.log", CreationCollisionOption::ReplaceExisting)).then([this](StorageFile^ file) {
stream = new std::ofstream(file->Path->Data());
}).wait();
}
if (dataWriter) {
dataWriter->WriteString(msg);
task<unsigned int>(dataWriter->StoreAsync()).wait();
if (stream) {
*stream << Utils::pstoccs(msg);
stream->flush();
}
}
#pragma once
#include "LinphoneCoreFactory.h"
#include <iostream>
#include <fstream>
using namespace Windows::Storage;
using namespace Windows::Storage::Streams;
namespace Linphone
{
......@@ -22,8 +22,8 @@ namespace Linphone
BackgroundModeLogger();
~BackgroundModeLogger();
StorageFile^ storageFile;
DataWriter^ dataWriter;
std::ofstream *stream;
std::recursive_mutex lock;
};
}
}
\ No newline at end of file
......@@ -156,7 +156,6 @@ namespace Linphone.Model
}
// Disconnect the listeners to prevent crash of the background process
BackgroundModeDebug();
server.LinphoneCore.CoreListener = null;
BackgroundProcessConnected = false;
......@@ -206,7 +205,8 @@ namespace Linphone.Model
/// </summary>
public void InitLinphoneCore()
{
ForegroundModeDebug();
server.LinphoneCoreFactory.SetDebugMode(SettingsManager.isDebugEnabled, server.BackgroundModeLogger);
Logger.Instance.TraceListener = server.BackgroundModeLogger;
if (server.LinphoneCore != null)
{
......@@ -223,20 +223,6 @@ namespace Linphone.Model
CallController.UnmuteRequested += UnmuteRequested;
}
#region Debug handling
private void ForegroundModeDebug()
{
Logger.Instance.Enable = SettingsManager.isDebugEnabled;
server.LinphoneCoreFactory.SetDebugMode(SettingsManager.isDebugEnabled, Logger.Instance);
}
private void BackgroundModeDebug()
{
Logger.Instance.Enable = SettingsManager.isDebugEnabled;
server.LinphoneCoreFactory.SetDebugMode(SettingsManager.isDebugEnabled, server.BackgroundModeLogger);
}
#endregion
#region CallLogs
private List<CallLogs> _history;
......
using Linphone.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
namespace Linphone.Model
{
/// <summary>
/// Class to handle the logging of the application
/// </summary>
public sealed class Logger : OutputTraceListener
public sealed class Logger
{
/// <summary>
/// Type of outputs to write the logs to
/// </summary>
[Flags] public enum Output
{
/// <summary>Write to a file synchronously</summary>
FILE_SYNCHRONOUS,
/// <summary>Write to the standard debug output (Visual Studio output)</summary>
DEBUG_WRITE
};
/// <summary>
/// The outputs to which to write the logs
/// </summary>
public Output Outputs { get; set; }
/// <summary>
/// The name of the file to write the logs to if using the FILE_SYNCHRONOUS output
/// The trace listener to which to send the messages to be logged
/// </summary>
public String Filename { get; set; }
/// <summary>
/// Whether the logger is enabled or not
/// </summary>
public bool Enable { get; set; }
public OutputTraceListener TraceListener { get; set; }
private Logger()
{
Outputs = Output.FILE_SYNCHRONOUS;
Filename = "Linphone.log";
}
private static Logger singleton;
......@@ -66,44 +37,16 @@ namespace Linphone.Model
}
}
private StorageFile storageFile;
private StreamWriter streamWriter;
private async Task<bool> CreateFileIfNeeded()
{
if (Filename == null) return false;
if (streamWriter == null)
{
StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
storageFile = await localFolder.CreateFileAsync(Filename, CreationCollisionOption.ReplaceExisting);
streamWriter = new StreamWriter(storageFile.Path);
}
return true;
}
private async void WriteToFile(String msg)
{
bool fileExists = await CreateFileIfNeeded();
if (fileExists)
{
streamWriter.WriteLine(msg);
streamWriter.Flush();
}
}
/// <summary>
/// Write a message to the configured outputs
/// Write a message to the logs
/// </summary>
/// <param name="level">The trace level of the message to be written</param>
/// <param name="msg">The message to be written</param>
private void Write(OutputTraceLevel level, String msg)
{
if (Outputs.HasFlag(Output.FILE_SYNCHRONOUS))
if (TraceListener != null)
{
WriteToFile(msg);
}
if (Outputs.HasFlag(Output.DEBUG_WRITE))
{
Debug.WriteLine(msg);
TraceListener.OutputTrace(level, msg);
}
}
......@@ -142,13 +85,5 @@ namespace Linphone.Model
{
Logger.Instance.Write(OutputTraceLevel.Error, msg);
}
/// <summary>
/// Handler to get and output native traces
/// </summary>
public void OutputTrace(OutputTraceLevel level, String msg)
{
Write(level, msg);
}
}
}
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