Advertisement
SemlerPDX

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

May 1st, 2023
1,532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | Source Code | 0 0
  1. using System;
  2.  
  3. namespace VoiceAttack_Plugin_HelloWorld
  4. {
  5.     /// <summary>
  6.     /// A class for methods providing a means to long strings of data to the VoiceAttack Event Log without being truncated.
  7.     /// </summary>
  8.     public static class Logging
  9.     {
  10.         /// <summary>
  11.         /// A method to write very long messages to the VoiceAttack Event Log, wrapping text
  12.         /// which exceeds the minimum width of the window to a new event log entry on a new line.
  13.         /// <br /><br />
  14.         /// Use this method by passing the VA proxy object, the string to write, and the color (or "blank" for no color)
  15.         /// </summary>
  16.         /// <param name="VA">The dynamic object which provides VoiceAttack attributes and functionality for this plugin.</param>
  17.         /// <param name="longString">The text string containing the message to write to the Event Log without truncation.</param>
  18.         /// <param name="colorString">The color of the square to the left of each Event Log entry.</param>
  19.         public static void WriteToLog_Long(dynamic VA, string longString, string colorString)
  20.         {
  21.             try
  22.             {
  23.                 int maxWidth = 91;
  24.                 string[] lines = longString.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  25.  
  26.                 foreach (string line in lines)
  27.                 {
  28.                     if (line.Length <= maxWidth)
  29.                     {
  30.                         VA.WriteToLog(line, colorString);
  31.                         continue;
  32.                     }
  33.  
  34.                     int index = 0;
  35.                     while (index < line.Length)
  36.                     {
  37.                         try
  38.                         {
  39.                             int length = Math.Min(maxWidth, line.Length - index);
  40.  
  41.                             if (length == maxWidth)
  42.                             {
  43.                                 // Check if the line should wrap at a whitespace
  44.                                 int lastSpaceIndex = line.LastIndexOf(' ', index + length, length);
  45.                                 if (lastSpaceIndex != -1 && lastSpaceIndex != index + length)
  46.                                 {
  47.                                     length = lastSpaceIndex - index;
  48.                                 }
  49.                             }
  50.  
  51.                             VA.WriteToLog(line.Substring(index, length), colorString);
  52.                             index += length;
  53.  
  54.                             // Ignore whitespace at the beginning of a new line
  55.                             while (index < line.Length && char.IsWhiteSpace(line[index]))
  56.                             {
  57.                                 index++;
  58.                             }
  59.                         }
  60.                         catch (ArgumentOutOfRangeException)
  61.                         {
  62.                             // Catch ArgumentOutOfRangeException and continue the loop
  63.                             VA.WriteToLog("VoiceAttack Plugin Error: ArgumentOutOfRangeException caught at WriteToLog_Long() method", "red");
  64.  
  65.                             continue;
  66.                         }
  67.                     }
  68.                 }
  69.             }
  70.             catch
  71.             {
  72.                 VA.WriteToLog("VoiceAttack Plugin Error: Failure ignored at WriteToLog_Long() method", "red");
  73.             }
  74.         }
  75.  
  76.     }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement