Trace.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #if DEBUG
  2. # include "../Constants.h"
  3. # include "../LinJam.h"
  4. # include "Trace.h"
  5. /* Trace class private class variables */
  6. Array<String> Trace::UnknowwnChannels = Array<String>() ;
  7. /* Trace class public class methods */
  8. void Trace::TraceEvent(String msg) { if (DEBUG_TRACE_EVENTS) DBG("[EVENT]: " + msg) ; }
  9. void Trace::TraceConfig(String msg) { if (DEBUG_TRACE_STATE) DBG("[CONFIG]: " + msg) ; }
  10. void Trace::TraceClient(String msg) { if (DEBUG_TRACE_STATE) DBG("[CLIENT]: " + msg) ; }
  11. void Trace::TraceServer(String msg) { if (DEBUG_TRACE_STATE) DBG("[SERVER]: " + msg) ; }
  12. void Trace::TraceGui(String msg) { if (DEBUG_TRACE_EVENTS) DBG("[GUI]: " + msg) ; }
  13. void Trace::TraceVerbose(String msg) { if (DEBUG_TRACE_VB) DBG("[DEBUG]: " + msg) ; }
  14. #ifndef DEBUG_ANSI_COLORS
  15. void Trace::TraceState(String msg) { if (DEBUG_TRACE_STATE) DBG("[STATE]: " + msg) ; }
  16. void Trace::TraceError(String msg) { if (DEBUG_TRACE_STATE) DBG("[ERROR]: " + msg) ; }
  17. // void Trace::TraceNetwork(String msg) { if (DEBUG_TRACE_EVENTS) DBG("[NETWORK]: " + msg) ; }
  18. #else // DEBUG_ANSI_COLORS
  19. void Trace::TraceState(String msg)
  20. { if (DEBUG_TRACE_STATE) DBG("\033[1;33m[STATE]: " + msg + "\033[0m") ; }
  21. void Trace::TraceError(String msg)
  22. { if (DEBUG_TRACE_STATE) DBG("\033[0;31m[ERROR]: " + msg + "\033[0m") ; }
  23. // void Trace::TraceNetwork(String msg)
  24. // { if (DEBUG_TRACE_EVENTS) DBG("\033[0;32m[NETWORK]: " + msg + "\033[0m") ; }
  25. #endif // DEBUG_ANSI_COLORS
  26. void Trace::DumpStoreXml(ValueTree store)
  27. { DBG(String(store.getType()) + " xml=\n" + store.toXmlString()) ; }
  28. String Trace::DumpStoredChannels()
  29. {
  30. String dump = " localChannels =>" ;
  31. ValueTree channels = LinJam::Config->localChannels ;
  32. ValueTree users = LinJam::Config->remoteUsers ;
  33. for (int ch_n = 0 ; ch_n < channels.getNumChildren() ; ++ch_n)
  34. dump += "\n " + channels.getChild(ch_n)[CONFIG::CHANNEL_NAME_ID].toString() ;
  35. dump += "\n remoteChannels =>" ;
  36. for (int user_n = 0 ; user_n < users.getNumChildren() ; ++user_n)
  37. {
  38. channels = users.getChild(user_n) ;
  39. /* TODO: KLUDGE (issue #33) nyi remote-channels node */
  40. if (!channels.hasProperty(CONFIG::USER_IDX_ID)) continue ;
  41. for (int ch_n = 0 ; ch_n < channels.getNumChildren() ; ++ch_n)
  42. dump += "\n " + String(channels.getType()) + " " +
  43. channels.getChild(ch_n)[CONFIG::CHANNEL_NAME_ID].toString() ;
  44. }
  45. return dump ;
  46. }
  47. String Trace::DumpClientChannels()
  48. {
  49. String dump = "\n client locals =>" ; int channel_n = -1 ; int channel_idx ;
  50. while (~(channel_idx = LinJam::Client->EnumLocalChannels(++channel_n)))
  51. dump += "\n " + LinJam::GetLocalChannelClientName(channel_idx) ;
  52. dump =+ "\n client remotes =>" ;
  53. int user_idx = LinJam::Client->GetNumUsers() ; String user_name ;
  54. while ((user_name = LinJam::GetRemoteUserName(--user_idx)).isNotEmpty())
  55. {
  56. dump += "\n " + user_name + " =>" ;
  57. int channel_n = -1 ; int channel_idx ;
  58. while (~(channel_idx = LinJam::Client->EnumUserChannels(user_idx , ++channel_n)))
  59. dump += "\n " + LinJam::GetRemoteChannelClientName(user_idx , channel_idx) ;
  60. }
  61. return dump ;
  62. }
  63. String Trace::SanitizeConfig(ValueTree default_config , ValueTree stored_config ,
  64. String pad)
  65. {
  66. if (!default_config.isValid()) return "ERROR: default config invalid" ;
  67. if (!stored_config.isValid()) return "ERROR: stored config invalid" ;
  68. Identifier node_name = default_config.getType() ;
  69. int n_default_children = default_config.getNumChildren() ;
  70. int n_stored_children = stored_config.getNumChildren() ;
  71. int n_unique_children = n_default_children ;
  72. if (stored_config.isValid())
  73. for (int child_n = 0 ; child_n < stored_config.getNumChildren() ; ++child_n)
  74. {
  75. Identifier node_name = stored_config.getChild(child_n).getType() ;
  76. if (!default_config.getChildWithName(node_name).isValid())
  77. ++n_unique_children ;
  78. }
  79. String dbg = "\n" + pad + "default node => " + String(node_name) + " (" +
  80. String(n_default_children) + " default " +
  81. String(n_stored_children) + " stored " +
  82. String(n_unique_children) + " unique children)" ;
  83. for (int child_n = 0 ; child_n < default_config.getNumChildren() ; ++child_n)
  84. {
  85. ValueTree default_child = default_config.getChild(child_n) ;
  86. Identifier node_name = default_child.getType() ;
  87. ValueTree stored_child = stored_config.getChildWithName(node_name) ;
  88. int n_properties = default_child.getNumProperties() ;
  89. if (n_properties)
  90. {
  91. dbg += "\n" + pad + " default node => " + String(node_name) + " (" +
  92. ((n_properties)? String(n_properties) + " properties" : "empty") + ")" +
  93. ((!stored_child.isValid())? " - stored node n/a - adding" : "") ;
  94. for (int property_n = 0 ; property_n < n_properties ; ++property_n)
  95. {
  96. Identifier key = default_child.getPropertyName(property_n) ;
  97. String default_value = default_child.getProperty(key).toString() ;
  98. String stored_value = (!stored_child.isValid())? "n/a - adding" :
  99. stored_child.getProperty(key , "n/a - adding").toString() ;
  100. dbg += "\n" + pad + " key => " + String(key) +
  101. "\n" + pad + " default_value => " + default_value +
  102. "\n" + pad + " stored_value => " + stored_value ;
  103. }
  104. }
  105. else dbg += SanitizeConfig(default_child , stored_child , pad + " ") ;
  106. }
  107. Array<Identifier> user_keys ;
  108. user_keys.add(CONFIG::SUBSCRIPTIONS_ID) ;
  109. user_keys.add(CONFIG::SERVERS_ID) ;
  110. user_keys.add(CONFIG::MASTERS_ID) ;
  111. user_keys.add(CONFIG::LOCALS_ID) ;
  112. // user_keys.add(CONFIG::REMOTES_ID) ; // TODO: (issue #33)
  113. if (user_keys.contains(node_name))
  114. {
  115. for (int child_n = 0 ; child_n < stored_config.getNumChildren() ; ++child_n)
  116. {
  117. ValueTree stored_child = stored_config .getChild(child_n) ;
  118. Identifier node_name = stored_child .getType() ;
  119. ValueTree default_child = default_config.getChildWithName(node_name) ;
  120. int n_children = stored_child .getNumChildren() ;
  121. int n_properties = stored_child .getNumProperties() ;
  122. if (default_child.isValid() || n_children) continue ;
  123. dbg += "\n" + pad + " stored node => " + String(node_name) + " (" +
  124. ((n_properties)? String(n_properties) + " properties" : "empty") + ")" ;
  125. for (int property_n = 0 ; property_n < n_properties ; ++property_n)
  126. {
  127. Identifier key = stored_child.getPropertyName(property_n) ;
  128. String stored_value = stored_child.getProperty(key).toString() ;
  129. dbg += "\n" + pad + " key => " + String(key) +
  130. "\n" + pad + " stored_value => " + stored_value ;
  131. }
  132. }
  133. }
  134. return dbg ;
  135. }
  136. void Trace::TraceInvalidNode(String a_node_name)
  137. { Trace::TraceError("node '" + a_node_name + "' invalid") ; }
  138. void Trace::TraceMissingValue(String a_node_name , String a_value_name)
  139. { Trace::TraceError("node '" + a_node_name + "' - missing key '" + a_value_name + "'") ; }
  140. void Trace::TraceMissingProperty(String a_node_name , String a_property_name ,
  141. String parent_node_name )
  142. {
  143. if (parent_node_name.isNotEmpty()) parent_node_name += " " ;
  144. Trace::TraceError(parent_node_name + "node '" + a_node_name +
  145. "' - missing property '" + a_property_name + "'") ;
  146. }
  147. void Trace::TraceTypeMismatch(ValueTree a_node , String a_property_name ,
  148. String expected_type , var a_var ,
  149. String parent_node_name )
  150. {
  151. String a_node_name = String(a_node.getType()) ;
  152. if (parent_node_name.isNotEmpty()) parent_node_name += " " ;
  153. Trace::TraceError("type mismatch - " + parent_node_name + a_node_name +
  154. "[" + a_property_name + "] => " +
  155. VarType(a_var) + " (expected " + expected_type + ")") ;
  156. }
  157. String Trace::VarType(var a_var)
  158. {
  159. String dynamic_type = "unknown" ;
  160. if (a_var.isVoid()) dynamic_type = "void" ;
  161. else if (a_var.isUndefined()) dynamic_type = "undefined" ;
  162. else if (a_var.isInt()) dynamic_type = "int" ;
  163. else if (a_var.isInt64()) dynamic_type = "int64" ;
  164. else if (a_var.isBool()) dynamic_type = "bool" ;
  165. else if (a_var.isDouble()) dynamic_type = "double" ;
  166. else if (a_var.isString()) dynamic_type = "string" ;
  167. else if (a_var.isObject()) dynamic_type = "object" ;
  168. else if (a_var.isArray()) dynamic_type = "array" ;
  169. else if (a_var.isBinaryData()) dynamic_type = "binary" ;
  170. else if (a_var.isMethod()) dynamic_type = "method" ;
  171. return dynamic_type ;
  172. }
  173. String Trace::DumpVar(String val_name , var a_var)
  174. {
  175. return "key => " + val_name .paddedRight(' ' , 20) +
  176. " type => " + VarType(a_var).paddedRight(' ' , 10) +
  177. " value => " + a_var.toString() ;
  178. }
  179. String Trace::Status2String(int status)
  180. {
  181. return (status == -9)? "LINJAM_STATUS_INIT" :
  182. (status == -8)? "LINJAM_STATUS_AUDIOERROR" :
  183. (status == -7)? "LINJAM_STATUS_CONFIGPENDING" :
  184. (status == -6)? "LINJAM_STATUS_READY" :
  185. (status == -5)? "LINJAM_STATUS_LICENSEPENDING" :
  186. (status == -4)? "LINJAM_STATUS_ROOMFULL" :
  187. (status == -3)? "NJC_STATUS_DISCONNECTED" :
  188. (status == -2)? "NJC_STATUS_INVALIDAUTH" :
  189. (status == -1)? "NJC_STATUS_CANTCONNECT" :
  190. (status == 0)? "NJC_STATUS_OK" :
  191. (status == 1)? "NJC_STATUS_PRECONNECT" :
  192. "(unknown: " + String(status) + ")" ;
  193. }
  194. #endif // #if DEBUG