staafl

multicat.cs

Dec 20th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.IO;
  5.  
  6. // usage: multicat.exe - logfile1.log logfile2.log
  7. // '-' means STDOUT
  8. class Program
  9. {
  10. public static void Main(string[] args)
  11. {
  12. try
  13. {
  14. foreach(var path in args)
  15. {
  16. if (path == "-")
  17. writers[path] = Console.Out;
  18. else
  19. writers[path] = new StreamWriter(path);
  20. }
  21.  
  22. string input;
  23. while ((input = Console.ReadLine()) != null)
  24. {
  25. foreach (var writer in writers.Values)
  26. {
  27. writer.WriteLine(input);
  28. }
  29. }
  30. }
  31. finally
  32. {
  33. foreach (var kvp in writers)
  34. {
  35. if (kvp.Key != "-")
  36. kvp.Value.Dispose();
  37. }
  38. }
  39. }
  40.  
  41. static readonly IDictionary<string, TextWriter> writers = new SortedDictionary<string, TextWriter>();
  42. }
Advertisement
Add Comment
Please, Sign In to add comment