// RockUp
// Commands-Logs -- Tail service logs

var Config = require('../lib/Config');

module.exports = LogsCommand;

function LogsCommand (program) {
  program
    .command("logs <environment>")
    .description("Tail service logs")
    .option("-H, --host <name>", "Tails logs on an individual host")
    .option("--service <name>", "Limits output to one running service")
    .action( function(environment, cliOptions) {
      var config = Config._loadLocalConfigFile(environment);
      var tailOptions = "-F -n50";

      var hosts;
      if ( cliOptions.host ) {
        var host = config.hosts.get(cliOptions.host);
        if (!host) {
          console.error("Cannot find host:".red.bold, cliOptions.host, "\n");
          process.exit(1);
        }
        hosts = [host];
      } else {
        hosts = config.hosts.list;
      }

      var limitService = cliOptions.service;

      console.log("Tailing hosts:", _.pluck(hosts, 'name').join(', '));
      console.log("Tailing services:", limitService || 'all');

      _.each(hosts, function(host) {
        var session = host.sshSession({keepAlive: false});
        host.services.each( function(service) {
          if (limitService && service.name !== limitService)
            return;
          var prefix = "["+host.name+":".white+service.name.cyan+"] ";
          var tailCommand = "sudo tail "+tailOptions+" /var/log/upstart/"+service.name+".log";
          session.execute(tailCommand, {
            onStdout: function(data) {
              if (data.toString())
                process.stdout.write("\n" + prefix.magenta + data.toString());
            },
            onStderr: function(data) {
              if (data.toString())
                process.stderr.write("\n" + prefix.magenta + data.toString());
            }
          }); // execute
        }); // services
      }); // hosts

    });

  return program;
}

@@ -42,16 +42,44 @@
 e logs%0A%0A
+var path = require('path');%0A
 var Conf
@@ -339,16 +339,20 @@
 option(%22
+-S, 
 --servic
@@ -515,43 +515,8 @@
 nt);
-%0A      var tailOptions = %22-F -n50%22;
 %0A%0A  
@@ -1124,85 +1124,210 @@
     
-host.services.each( function(service) %7B%0A          if (limitService &&
+var serviceNames = limitService ? %5BlimitService%5D : host.services.names;%0A        _tailLogs(session, %22-F%22, serviceNames);%0A      %7D); // hosts%0A%0A    %7D);%0A%0A  return program;%0A%7D%0A%0A/**%0A * Given an array of
  service
 .nam
@@ -1326,131 +1326,498 @@
 vice
-.
+ 
 name
- !== limitService)%0A            return;%0A          var prefix = %22%5B%22+host.name+%22:%22.white+
+s, connect to and tail the logs%0A * for all services simultaneously.%0A *%0A * @param %7BSession%7D session       Nodemiral session to host%0A * @param %7BString%7D tailOptions    Command args to tail on server%0A * @param %7BArray%7D serviceNames    Names of Rock services to tail logs%0A **/%0Afunction _tailLogs (session, tailOptions, serviceNames) %7B%0A  var fileNames = _(
 service
-.name.cyan+%22%5D %22;%0A        
+Names).map( function(sn) %7B %0A    return path.resolve(%22/var/log/upstart%22, sn+%22.log%22);%0A  %7D);%0A  var pathList = fileNames.join(' ');%0A%0A
   va
@@ -1826,31 +1826,33 @@
 tail
-Command = %22
+Parts = %5B'
 sudo
- 
+', '
 tail
- %22+
+', 
 tail
@@ -1862,59 +1862,63 @@
 ions
-+%22 /var/log/upstart/%22+service.name+%22.log%22;%0A        
+, pathList%5D;%0A  var tailCommand = tailParts.join(' ');%0A%0A
   se
@@ -1946,24 +1946,16 @@
 mand, %7B%0A
-        
     onSt
@@ -1973,32 +1973,24 @@
 ion(data) %7B%0A
-        
       if (da
@@ -2008,32 +2008,24 @@
 ())%0A        
-        
 process.stdo
@@ -2037,73 +2037,33 @@
 ite(
-%22%5Cn%22 + prefix.magenta + data.toString());%0A            %7D,%0A        
+data.toString());%0A    %7D,%0A
     
@@ -2089,24 +2089,16 @@
 data) %7B%0A
-        
       if
@@ -2124,24 +2124,16 @@
         
-        
 process.
@@ -2149,158 +2149,47 @@
 ite(
-%22%5Cn%22 + prefix.magenta + data.toString());%0A            %7D%0A          %7D); // execute%0A        %7D); // services%0A      %7D); // hosts%0A%0A    %7D);%0A%0A  return program;
+data.toString());%0A    %7D%0A  %7D); // execute
 %0A%7D%0A

Use tail's muxing for service logs