Skip to content

Commit

Permalink
Merge branch 'feature/11' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomiejwolk committed May 2, 2015
2 parents a183603 + e193743 commit b5bb351
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 56 deletions.
15 changes: 7 additions & 8 deletions Editor/InspectorControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,31 @@ namespace FileLogger {
public static class InspectorControls {

public static bool DrawStartStopButton(
// todo rename to loggerState
bool oldLoggingEnabledValue,
bool loggerState,
bool enableOnPlay,
Action<bool> stateChangedCallback) {

var btnText = GetStartStopButtonText(
oldLoggingEnabledValue,
loggerState,
enableOnPlay);

// Draw button.
var btnState = GUILayout.Toggle(
oldLoggingEnabledValue,
loggerState,
btnText,
"Button");

// Execute callback.
if (btnState != oldLoggingEnabledValue) {
if (btnState != loggerState) {
if (stateChangedCallback != null) {
stateChangedCallback(btnState);
}
}

// Return button state.
return btnState == oldLoggingEnabledValue
? oldLoggingEnabledValue
: !oldLoggingEnabledValue;
return btnState == loggerState
? loggerState
: !loggerState;
}

private static string GetStartStopButtonText(
Expand Down
17 changes: 17 additions & 0 deletions Editor/LoggerEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class LoggerEditor : Editor {
private SerializedProperty logInRealTime;
private SerializedProperty methodFilter;
private SerializedProperty qualifiedClassName;
private SerializedProperty append;

#endregion SERIALIZED PROPERTIES

Expand All @@ -44,6 +45,7 @@ public class LoggerEditor : Editor {

DrawEnableOnPlayToggle();
DrawLogInRealTimeToggle();
DrawAppendToggle();
DrawEchoToConsoleToggle();

EditorGUILayout.Space();
Expand Down Expand Up @@ -92,6 +94,7 @@ public class LoggerEditor : Editor {
indentLine = serializedObject.FindProperty("indentLine");
classFilter = serializedObject.FindProperty("classFilter");
methodFilter = serializedObject.FindProperty("methodFilter");
append = serializedObject.FindProperty("append");
}

#endregion UNITY MESSAGES
Expand All @@ -108,6 +111,20 @@ public class LoggerEditor : Editor {
Script.DisplayOptions);
}

private void DrawAppendToggle() {
var disabled = logInRealTime.boolValue ? true : false;

EditorGUI.BeginDisabledGroup(disabled);

append.boolValue = EditorGUILayout.Toggle(
new GUIContent(
"Always Append",
"Always append messages to the log file."),
append.boolValue);

EditorGUI.EndDisabledGroup();
}

private void DrawClearLogFileButton() {
// Don't allow reseting log file while logging.
if (Script.LoggingEnabled) return;
Expand Down
21 changes: 16 additions & 5 deletions LogWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class LogWriter {
set { loggedMessages = value; }
}

/// <summary>
/// Event called on every write to the file.
/// </summary>
public static event WriteEventHandler WriteEvent;

/// Save log message to cache.
Expand All @@ -56,11 +59,7 @@ public class LogWriter {
}
// Cache message.
logCache[logIdx] = message;
// todo this should be done in the Log()
// Handle "Echo To Console" inspector option.
if (echoToConsole) {
Debug.Log(message);
}

logIdx += 1;
loggedMessages += 1;
// Resize array when needed.
Expand Down Expand Up @@ -105,6 +104,18 @@ public class LogWriter {
OnWriteEvent(e);
}

public void WriteSingle(string message, string filePath, bool append) {
// Create stream writer used to write log cache to file.
using (writer = new StreamWriter(filePath, append)) {
writer.WriteLine(message);
}

// Fire event.
var e = new EventArgs();
OnWriteEvent(e);
}


protected void OnWriteEvent(EventArgs e) {
if (WriteEvent != null) {
WriteEvent(this, e);
Expand Down
116 changes: 73 additions & 43 deletions Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace FileLogger {
[ExecuteInEditMode]
public sealed class Logger : MonoBehaviour {
#region EVENTS

/// <summary>
/// Delegate for <c>StateChanged</c> event.
/// </summary>
Expand All @@ -39,16 +38,6 @@ public sealed class Logger : MonoBehaviour {
public static event StateChangedEventHandler StateChanged;

#endregion EVENTS

#region EVENT INVOCATORS

private void OnStateChanged(bool state) {
var handler = StateChanged;
if (handler != null) handler(this, state);
}

#endregion EVENT INVOCATORS

#region FIELDS

private static Logger instance;
Expand Down Expand Up @@ -224,6 +213,11 @@ public sealed class Logger : MonoBehaviour {
}
}

public bool EchoToConsole {
get { return echoToConsole; }
set { echoToConsole = value; }
}

public const string VERSION = "v0.1.0";

#endregion PROPERTIES
Expand Down Expand Up @@ -253,10 +247,11 @@ public sealed class Logger : MonoBehaviour {
if (LogInRealTime) {
return;
}

// Write log to file when 'enableOnPlay' was selected.
if (enableOnPlay) {
// Write single message to the file.
logWriter.WriteAll(filePath, append);
logWriter.WriteAll(FilePath, Append);
}
}

Expand All @@ -274,31 +269,42 @@ public sealed class Logger : MonoBehaviour {
UnsubscribeFromEvents();
SubscribeToEvents();
}

// todo move to region
void Logger_StateChanged(object sender, bool state) {
if (!state) LogWriter.WriteAll(FilePath, false);
}

private void OnDisable() {
UnsubscribeFromEvents();
}
#endregion UNITY MESSAGES

// todo move to region
private void SubscribeToEvents() {
UnityEngine.Debug.Log("SubscribeToEvents");
StateChanged += Logger_StateChanged;
#region EVENT INVOCATORS

private void OnStateChanged(bool state) {
var handler = StateChanged;
if (handler != null) handler(this, state);
}

// todo move to region
#endregion EVENT INVOCATORS

#region EVENT HANDLERS
void Logger_StateChanged(object sender, bool state) {
// There's no need to write cached messages since logging was made
// in real time.
if (Instance.LogInRealTime) return;

// Save messages to file on logger stop.
if (!state) LogWriter.WriteAll(FilePath, Append);
}
#endregion

#region METHODS
private void UnsubscribeFromEvents() {
UnityEngine.Debug.Log("UnsubscribeFromEvents");
StateChanged -= Logger_StateChanged;
}

#endregion UNITY MESSAGES
private void SubscribeToEvents() {
UnityEngine.Debug.Log("SubscribeToEvents");
StateChanged += Logger_StateChanged;
}

#region METHODS

[Conditional("DEBUG_LOGGER")]
public static void LogCall() {
Expand All @@ -311,15 +317,17 @@ public sealed class Logger : MonoBehaviour {
}

private static void DoLogCall(object objectReference) {
// Return if method is disabled.
if (!FlagsHelper.IsSet(
Instance.EnabledMethods,
EnabledMethods.LogCall)) return;

// Get info from call stack.
var stackInfo = new FrameInfo(3);

Log(
stackInfo.MethodSignature,
stackInfo,
FlagsHelper.IsSet(
Instance.EnabledMethods,
EnabledMethods.LogCall),
objectReference);
}

Expand All @@ -334,6 +342,11 @@ public sealed class Logger : MonoBehaviour {
}

private static void DoLogResult(object result, object objectRererence) {
// Return if method is disabled.
if (!FlagsHelper.IsSet(
Instance.EnabledMethods,
EnabledMethods.LogResult)) return;

// Compose log message.
var message = string.Format("[RESULT: {0}]", result);

Expand All @@ -344,9 +357,6 @@ public sealed class Logger : MonoBehaviour {
Log(
message,
stackInfo,
FlagsHelper.IsSet(
Instance.EnabledMethods,
EnabledMethods.LogResult),
objectRererence);
}

Expand All @@ -355,6 +365,8 @@ public sealed class Logger : MonoBehaviour {
/// </summary>
[Conditional("DEBUG_LOGGER")]
public static void LogStackTrace() {
if (!Instance.enableLogStackTrace) return;

var stackTrace = new StackTrace();
var message = new StringBuilder();
for (var i = 1; i < stackTrace.FrameCount; i++) {
Expand All @@ -375,7 +387,6 @@ public sealed class Logger : MonoBehaviour {
Log(
message.ToString(),
stackInfo,
Instance.enableLogStackTrace,
null);
}

Expand All @@ -401,6 +412,11 @@ public sealed class Logger : MonoBehaviour {
object objectReference,
params object[] paramList) {

// Return if method is disabled.
if (!FlagsHelper.IsSet(
Instance.EnabledMethods,
EnabledMethods.LogString)) return;

// Compose log message.
var message = string.Format(format, paramList);

Expand All @@ -411,9 +427,6 @@ public sealed class Logger : MonoBehaviour {
Log(
message,
stackInfo,
FlagsHelper.IsSet(
Instance.EnabledMethods,
EnabledMethods.LogString),
objectReference);
}

Expand Down Expand Up @@ -445,8 +458,8 @@ public sealed class Logger : MonoBehaviour {

// Write single message to the file.
Instance.logWriter.WriteAll(
Instance.filePath,
Instance.append);
Instance.FilePath,
Instance.Append);
}

/// <summary>
Expand Down Expand Up @@ -610,11 +623,8 @@ public sealed class Logger : MonoBehaviour {
private static void Log(
string message,
FrameInfo frameInfo,
bool methodEnabled,
object objectReference) {

// todo this check should be executed inside each of the DoLog methods.
if (!methodEnabled) return;
if (!Instance.LoggingEnabled) return;

// Filter by class name.
Expand All @@ -640,14 +650,34 @@ public sealed class Logger : MonoBehaviour {
// Append caller class name.
HandleAppendCallerClassName(outputMessage);

HandleEchoToConsole(outputMessage);
HandleLogInRealTime(outputMessage);

// There's no need to write cached messages since logging was made
// in real time.
if (Instance.LogInRealTime) return;

// Add log message to the cache.
Instance.logWriter.AddToCache(
outputMessage.ToString(),
Instance.echoToConsole);
Instance.EchoToConsole);
}

// Append message to the log file.
private static void HandleLogInRealTime(StringBuilder outputMessage) {

// Append message to the log file.
if (Instance.LogInRealTime) {
Instance.logWriter.WriteLast(Instance.filePath);
Instance.logWriter.WriteSingle(
outputMessage.ToString(),
Instance.filePath,
true);
}
}

private static void HandleEchoToConsole(StringBuilder outputMessage) {

if (Instance.EchoToConsole) {
UnityEngine.Debug.Log(outputMessage.ToString());
}
}

Expand Down

0 comments on commit b5bb351

Please sign in to comment.