FileTarget.as 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright (c) 2008, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. 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 Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. 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. package com.adobe.air.logging
  28. {
  29. import mx.logging.AbstractTarget;
  30. import flash.filesystem.File;
  31. import flash.filesystem.FileStream;
  32. import flash.filesystem.FileMode;
  33. import mx.logging.LogEvent;
  34. import flash.system.System;
  35. import flash.system.Capabilities;
  36. import mx.logging.targets.LineFormattedTarget;
  37. import mx.core.mx_internal;
  38. use namespace mx_internal;
  39. /**
  40. * An Adobe AIR only class that provides a log target for the Flex logging
  41. * framework, that logs files to a file on the user's system.
  42. *
  43. * This class will only work when running within Adobe AIR>
  44. */
  45. public class FileTarget extends LineFormattedTarget
  46. {
  47. private const DEFAULT_LOG_PATH:String = "app-storage:/application.log";
  48. private var log:File;
  49. public function FileTarget(logFile:File = null)
  50. {
  51. if(logFile != null)
  52. {
  53. log = logFile;
  54. }
  55. else
  56. {
  57. log = new File(DEFAULT_LOG_PATH);
  58. }
  59. }
  60. public function get logURI():String
  61. {
  62. return log.url;
  63. }
  64. mx_internal override function internalLog(message:String):void
  65. {
  66. write(message);
  67. }
  68. private function write(msg:String):void
  69. {
  70. var fs:FileStream = new FileStream();
  71. fs.open(log, FileMode.APPEND);
  72. fs.writeUTFBytes(msg + "\n");
  73. fs.close();
  74. }
  75. public function clear():void
  76. {
  77. var fs:FileStream = new FileStream();
  78. fs.open(log, FileMode.WRITE);
  79. fs.writeUTFBytes("");
  80. fs.close();
  81. }
  82. }
  83. }