home *** CD-ROM | disk | FTP | other *** search
/ .net 1999 December / netCD65.iso / pc / Software / Top20 / DreamWeaver / PC / data1.cab / Program_Files / Configuration / Commands / errmsg.js < prev    next >
Encoding:
JavaScript  |  1998-11-30  |  6.0 KB  |  250 lines

  1. // 
  2. // Copyright 1998 Macromedia, Inc. All rights reserved.
  3. //
  4. // ErrMsg.js
  5. //
  6. // Logging functions for accumulating tracing output into
  7. // an array of "log pages" for eventual display in a
  8. // dialog box.  
  9. //
  10. // MM_enableLogging()
  11. // MM_disableLogging()
  12. // MM_note()
  13. // MM_error()
  14. // MM_assert()
  15. // MM_showLog()
  16. // MM_clearLog()
  17. // MM_newLogPage()
  18. //
  19. // MM_format()
  20. //
  21. // ------------ Package variables -------------
  22. //
  23. var _ErrMsg_bInitialized;
  24.  
  25. var _ErrMsg_bLogEnabled;
  26. var _ErrMsg_dtToday;
  27. var _ErrMsg_logPage;
  28. var _ErrMsg_logPageLines;
  29. var _ErrMsg_arrLogPages;
  30. var _ErrMsg_linesPerPage;
  31. var _ErrMsg_logPageHeader;
  32. var _ErrMsg_logPageHeaderLines;
  33.  
  34. var _ErrMsg_ErrorPrefix;
  35. var _ErrMsg_AssertPrefix;
  36.  
  37. // Initialize package variables here
  38. //
  39. function _ErrMsg_Initialize()
  40. {
  41.    if ( _ErrMsg_bInitialized == null )
  42.    {
  43.       // alert( "ErrMsg.js initializing" );
  44.    
  45.       _ErrMsg_bLogEnabled         = true;
  46.       _ErrMsg_ErrorPrefix         = "*E-: ";
  47.       _ErrMsg_AssertPrefix        = "*Assertion failure: ";
  48.       _ErrMsg_logPageHeader       = "Log page ";
  49.       _ErrMsg_logPageHeaderLines  = 3;
  50.       _ErrMsg_linesPerPage        = 25;
  51.       
  52.       _ErrMsg_initializeLog();
  53.       
  54.       _ErrMsg_bInitialized        = true;
  55.    }
  56. }
  57.  
  58. // ------------ External functions ------------
  59. //
  60. // Note: all external functions must call _ErrMsg_Initialize()
  61. // to initialize the package before using any package
  62. // global variables!
  63. //
  64.  
  65. function MM_enableLogging()
  66. {
  67.     _ErrMsg_Initialize();
  68.     _ErrMsg_bLogEnabled = true;
  69. }
  70.  
  71. function MM_disableLogging()
  72. {
  73.     _ErrMsg_Initialize();
  74.     _ErrMsg_bLogEnabled = false;
  75. }
  76.  
  77. // MM_note( str )
  78. //
  79. // Output an informational line to the log; at least one
  80. // string argument is required; optional arguments with
  81. // an initial format string containing '%s' tokens ala 
  82. // printf:
  83. //
  84. //       MM_note( "etc %s etc %s", str1, str2 )
  85. //
  86. // are also allowed
  87. //
  88. function MM_note( str )    // [, ...]
  89. {
  90.    _ErrMsg_Initialize();
  91.    if ( _ErrMsg_bLogEnabled )
  92.    {
  93.       if ( MM_note.arguments.length > 1 )
  94.          _ErrMsg_logPage += _ErrMsg_format( MM_note.arguments ) + "\n";
  95.       else   
  96.          _ErrMsg_logPage += str + "\n";
  97.       
  98.       if ( ++_ErrMsg_logPageLines > _ErrMsg_linesPerPage )
  99.          _ErrMsg_advanceLogPage();
  100.    }
  101. }
  102.  
  103. // MM_error( str )
  104. //
  105. // Output an error to the log; can take format string and
  106. // optional arguments like MM_note; see MM_note.
  107. //   
  108. function MM_error( str )   // [, ...]
  109. {
  110.    _ErrMsg_Initialize();
  111.    
  112.    if ( MM_error.arguments.length > 1 )
  113.       MM_note( _ErrMsg_ErrorPrefix + _ErrMsg_format( MM_error.arguments ) );
  114.    else
  115.       MM_note( _ErrMsg_ErrorPrefix + str );       
  116. }
  117.  
  118. // MM_assert()
  119. //
  120. // Debugging aid; failed assertions are noted in the log
  121. //
  122. function MM_assert( b, explanation )
  123. {
  124.    if ( !b )
  125.    {
  126.       _ErrMsg_Initialize();
  127.       MM_note( _ErrMsg_AssertPrefix + explanation );
  128.    }
  129. }
  130.  
  131. // MM_format()
  132. //
  133. // Return a formated string; arguments are printf-like,
  134. // e.g.:
  135. //       MM_format( "etc %s etc %s", str1, str2 )
  136. //
  137. function MM_format()
  138. {
  139.    _ErrMsg_Initialize();
  140.    return( _ErrMsg_format( MM_format.arguments ) );
  141. }
  142.   
  143. // MM_showLog()
  144. //
  145. // Display log pages in a pop-up dialog
  146. //      
  147. function MM_showLog()
  148. {
  149.    // To do: A dismissible dialog box (ala confirm() ) would 
  150.    // be nice to allow the cancelling of stepping through log 
  151.    // output at any page.
  152.    //
  153.    _ErrMsg_Initialize();
  154.    if ( _ErrMsg_bLogEnabled )
  155.    {
  156.       if ( _ErrMsg_logPageLines > _ErrMsg_logPageHeaderLines )
  157.          _ErrMsg_advanceLogPage();
  158.          
  159.       var nPages = _ErrMsg_arrLogPages.length;
  160.       for( var i = 0; i < nPages; i++ )
  161.          if ( alert( _ErrMsg_arrLogPages[i] ) )  // this doesn't work; alert always returns false
  162.             break;
  163.    }
  164. }      
  165.  
  166. // MM_clearLog()
  167. //
  168. // Throw away existing log and start a new one
  169. //
  170. function MM_clearLog()
  171. {
  172.    _ErrMsg_Initialize();
  173.    _ErrMsg_initializeLog();
  174. }
  175.  
  176. // MM_newLogPage()
  177. //
  178. // Advance the log page 
  179. //
  180. function MM_newLogPage()
  181. {
  182.    _ErrMsg_Initialize();
  183.    _ErrMsg_advanceLogPage();
  184. }
  185.  
  186. //
  187. // ------------ Internal functions ------------
  188. //
  189.  
  190. // Push the current log page on the log page array
  191. // and initialize a new current log page
  192. //
  193. function _ErrMsg_advanceLogPage()
  194. {
  195.    _ErrMsg_logPage      = _ErrMsg_arrLogPages.push( _ErrMsg_logPage );
  196.    _ErrMsg_logPage      = _ErrMsg_logPageHeader + (_ErrMsg_arrLogPages.length + 1 ) + "\n\n";
  197.    _ErrMsg_logPageLines = _ErrMsg_logPageHeaderLines;
  198. }
  199.  
  200. function _ErrMsg_initializeLog()
  201. {
  202.    _ErrMsg_dtToday      = new Date();
  203.    _ErrMsg_arrLogPages  = new Array();
  204.    _ErrMsg_logPage      = _ErrMsg_dtToday.toString() + "\n\n";
  205.    _ErrMsg_logPageLines = _ErrMsg_logPageHeaderLines;
  206. }
  207.  
  208. // Assemble an array of string arguments into a single
  209. // string to return.  The first string is should be
  210. // the format string with %s for string substitution
  211. // with the other elements of the array, e.g. like 
  212. // printf arguments collapsed into an array: 
  213. //       [ "blah blah %s blah %s", str1, str2 ]
  214. //
  215. function _ErrMsg_format( strArray )
  216. {
  217.    var nElements = strArray.length;
  218.    
  219.    if ( !nElements )
  220.       return "";
  221.       
  222.    if ( nElements == 1 )
  223.       return strArray[0];
  224.       
  225.    // Otherwise there's some formatting to do
  226.    //      
  227.    var strResult = "";
  228.    var arrIdx    = 0;
  229.    var startPos  = 0;
  230.    var strFormat = strArray[ arrIdx++ ];
  231.    var endPos    = strFormat.indexOf( "%s", startPos );
  232.  
  233.    if ( endPos == -1 )
  234.       return strFormat;
  235.       
  236.    while( startPos < strFormat.length )
  237.    {
  238.       strResult += strFormat.substring( startPos, endPos );
  239.       if ( arrIdx < nElements )
  240.          strResult += strArray[ arrIdx++ ];
  241.          
  242.       startPos = endPos + 2;
  243.       endPos   = strFormat.indexOf( "%s", startPos );
  244.                   
  245.       if ( endPos == -1 )
  246.          endPos = strFormat.length;      
  247.    }         
  248.       
  249.    return strResult;
  250. }