1
0

LogLinesAJAX.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using System.Text;
  32. using System.Text.RegularExpressions;
  33. using Mono.Data.SqliteClient;
  34. using OpenMetaverse;
  35. using OpenMetaverse.StructuredData;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Framework.Monitoring;
  38. namespace OpenSim.Region.UserStatistics
  39. {
  40. public class LogLinesAJAX : IStatsController
  41. {
  42. private Regex normalizeEndLines = new Regex(@"\r\n", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.Multiline);
  43. private Regex webFormat = new Regex(@"[^\s]*\s([^,]*),[^\s]*\s([A-Z]*)[^\s-][^\[]*\[([^\]]*)\]([^\n]*)",
  44. RegexOptions.Singleline | RegexOptions.Compiled);
  45. private Regex TitleColor = new Regex(@"[^\s]*\s(?:[^,]*),[^\s]*\s(?:[A-Z]*)[^\s-][^\[]*\[([^\]]*)\](?:[^\n]*)",
  46. RegexOptions.Singleline | RegexOptions.Compiled);
  47. #region IStatsController Members
  48. public string ReportName
  49. {
  50. get { return ""; }
  51. }
  52. public Hashtable ProcessModel(Hashtable pParams)
  53. {
  54. Hashtable nh = new Hashtable();
  55. nh.Add("loglines", pParams["LogLines"]);
  56. return nh;
  57. }
  58. public string RenderView(Hashtable pModelResult)
  59. {
  60. StringBuilder output = new StringBuilder();
  61. HTMLUtil.HR(ref output, "");
  62. output.Append("<H3>ActiveLog</H3>\n");
  63. string tmp = normalizeEndLines.Replace(pModelResult["loglines"].ToString(), "\n");
  64. string[] result = Regex.Split(tmp, "\n");
  65. string formatopen = "";
  66. string formatclose = "";
  67. for (int i = 0; i < result.Length; i++)
  68. {
  69. if (result[i].Length >= 30)
  70. {
  71. string logtype = result[i].Substring(24, 6);
  72. switch (logtype)
  73. {
  74. case "WARN ":
  75. formatopen = "<font color=\"#7D7C00\">";
  76. formatclose = "</font>";
  77. break;
  78. case "ERROR ":
  79. formatopen = "<font color=\"#FF0000\">";
  80. formatclose = "</font>";
  81. break;
  82. default:
  83. formatopen = "";
  84. formatclose = "";
  85. break;
  86. }
  87. }
  88. StringBuilder replaceStr = new StringBuilder();
  89. //string titlecolorresults =
  90. string formatresult = Regex.Replace(TitleColor.Replace(result[i], "$1"), "[^ABCDEFabcdef0-9]", "");
  91. if (formatresult.Length > 6)
  92. {
  93. formatresult = formatresult.Substring(0, 6);
  94. }
  95. for (int j = formatresult.Length; j <= 5; j++)
  96. formatresult += "0";
  97. replaceStr.Append("$1 - [<font color=\"#");
  98. replaceStr.Append(formatresult);
  99. replaceStr.Append("\">$3</font>] $4<br />");
  100. string repstr = replaceStr.ToString();
  101. output.Append(formatopen);
  102. output.Append(webFormat.Replace(result[i], repstr));
  103. output.Append(formatclose);
  104. }
  105. return output.ToString();
  106. }
  107. /// <summary>
  108. /// Return the last log lines. Output in the format:
  109. /// <pre>
  110. /// {"logLines": [
  111. /// "line1",
  112. /// "line2",
  113. /// ...
  114. /// ]
  115. /// }
  116. /// </pre>
  117. /// </summary>
  118. /// <param name="pModelResult"></param>
  119. /// <returns></returns>
  120. public string RenderJson(Hashtable pModelResult)
  121. {
  122. OSDMap logInfo = new OpenMetaverse.StructuredData.OSDMap();
  123. OSDArray logLines = new OpenMetaverse.StructuredData.OSDArray();
  124. string tmp = normalizeEndLines.Replace(pModelResult["loglines"].ToString(), "\n");
  125. string[] result = Regex.Split(tmp, "\n");
  126. for (int i = 0; i < result.Length; i++)
  127. {
  128. logLines.Add(new OSDString(result[i]));
  129. }
  130. logInfo.Add("logLines", logLines);
  131. return logInfo.ToString();
  132. }
  133. #endregion
  134. }
  135. }