Author Topic: C# Inline Function - Write to Event Log (with wrapping for long messages)  (Read 2877 times)

SemlerPDX

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 280
  • Upstanding Lunatic
    • My AVCS Homepage
Using the 'Write a Value to the Event Log' action can cause some longer messages to get cut off, including Exception messages that users may wish to print to the Event Log from inline functions.

This inline function will accept a message and a message indicator color as inputs (along with the VA proxy object) in order to allow writing very long messages to the Event Log without cutting off text, wrapping to the next line when too long.


Pastebin link:  https://pastebin.com/0Cmaf8bw

Code: [Select]
// Write to Event Log (Long)
// for VoiceAttack profile developers
//
// by SemlerPDX Apr2023
// VETERANS-GAMING.COM/AVCS

using System;

public class VAInline
{
  public void main()
  {
    string message = VA.ParseTokens("{CLIP}"); // example input - copy a paragraph of text to clipboard and call this function
    WriteToLog_Long(VA, message, "grey");
  }
 
  /// <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.
  /// </summary>
  /// <param name="VA">The dynamic object which provides VoiceAttack attributes and functionality for this method.</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;
              }
            }

            OpenAIplugin.VA_Proxy.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
            continue;
          }
        }
      }
    }
    catch
    {
      VA.WriteToLog("Inline Function Error: Failure ignored at WriteToLog_Long() method", "red");
    }
  }
}
« Last Edit: May 11, 2023, 11:47:13 AM by SemlerPDX »