1. /**
  2. * Encodes log file output for backward compatibility.
  3. *
  4. * @param {string} logData The raw log file data.
  5. * @returns {string} The encoded log data.
  6. */
  7. function encodeLogData(logData) {
  8. if (!logData) {
  9. return ""; // Handle empty input
  10. }
  11. let encodedData = "";
  12. let inString = false;
  13. for (let i = 0; i < logData.length; i++) {
  14. const char = logData[i];
  15. if (char === '"' && !inString) {
  16. // Start of a string, escape quotes
  17. encodedData += '"';
  18. inString = true;
  19. } else if (char === '"' && inString) {
  20. // End of a string, unescape quotes
  21. encodedData += '"';
  22. inString = false;
  23. } else if (char === '\\') {
  24. // Escape character, need to escape the next character
  25. encodedData += '\\';
  26. if (i + 1 < logData.length) {
  27. encodedData += logData[i + 1];
  28. i++; // Skip the escaped character
  29. }
  30. } else {
  31. encodedData += char;
  32. }
  33. }
  34. return encodedData;
  35. }
  36. // Example usage (for testing)
  37. // const logData = `This is a log message with "quotes" and \\escaped characters.`;
  38. // const encodedLog = encodeLogData(logData);
  39. // console.log(encodedLog);

Add your comment