SemlerPDX icon

VoiceAttack C# Plugin Class - Write to Event Log (Long)

SemlerPDX | PRO | 05/01/23 11:38:20 PM UTC | 0 ⭐ | 1933 👁️ | Never ⏰ | [C#, Plugin, class, csharp, VoiceAttack]
C# |

3.36 KB

|

Source Code

|

0 👍

/

0 👎

using System;
 
namespace VoiceAttack_Plugin_HelloWorld
{
    /// <summary>
    /// A class for methods providing a means to long strings of data to the VoiceAttack Event Log without being truncated.
    /// </summary>
    public static class Logging
    {
        /// <summary>
        /// A method to write very long messages to the VoiceAttack Event Log, wrapping text
        /// which exceeds the minimum width of the window to a new event log entry on a new line.
        /// <br /><br />
        /// Use this method by passing the VA proxy object, the string to write, and the color (or "blank" for no color)
        /// </summary>
        /// <param name="VA">The dynamic object which provides VoiceAttack attributes and functionality for this plugin.</param>
        /// <param name="longString">The text string containing the message to write to the Event Log without truncation.</param>
        /// <param name="colorString">The color of the square to the left of each Event Log entry.</param>
        public static void WriteToLog_Long(dynamic VA, string longString, string colorString)
        {
            try
            {
                int maxWidth = 91;
                string[] lines = longString.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
 
                foreach (string line in lines)
                {
                    if (line.Length <= maxWidth)
                    {
                        VA.WriteToLog(line, colorString);
                        continue;
                    }
 
                    int index = 0;
                    while (index < line.Length)
                    {
                        try
                        {
                            int length = Math.Min(maxWidth, line.Length - index);
 
                            if (length == maxWidth)
                            {
                                // Check if the line should wrap at a whitespace
                                int lastSpaceIndex = line.LastIndexOf(' ', index + length, length);
                                if (lastSpaceIndex != -1 && lastSpaceIndex != index + length)
                                {
                                    length = lastSpaceIndex - index;
                                }
                            }
 
                            VA.WriteToLog(line.Substring(index, length), colorString);
                            index += length;
 
                            // Ignore whitespace at the beginning of a new line
                            while (index < line.Length && char.IsWhiteSpace(line[index]))
                            {
                                index++;
                            }
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            // Catch ArgumentOutOfRangeException and continue the loop
                            VA.WriteToLog("VoiceAttack Plugin Error: ArgumentOutOfRangeException caught at WriteToLog_Long() method", "red");
 
                            continue;
                        }
                    }
                }
            }
            catch
            {
                VA.WriteToLog("VoiceAttack Plugin Error: Failure ignored at WriteToLog_Long() method", "red");
            }
        }
 
    }
}
 

Comments