使用 xxx > log,我们可以将所有控制台打印到日志文件中。
虽然我怎样才能获得每张照片的时间戳?
谢谢
构建一个脚本,将输入行回显到标准输出,并在前面加上时间戳。您可以使用 shell/bash、perl、python、ruby、C、awk 等(读取和写入 stdio 并获取格式化日期的任何东西),
#!/usr/bin/bash
#loggy.sh
while read line
do
now=$(/bin/date +%y%m%d%H%M%S)
echo $now $line
end
例子,
echo "hello, world" | ~/loggy.sh
更喜欢perl?
#!/bin/env perl
use strict;
use warnings;
#loggy - log a line, prepend date
while(my $line=<>)
{
my $now=`/bin/date +%y%m%d%H%M%S`; chomp($now);
print "$now: $line";
}
红宝石呢?
#!/bin/env ruby
#loggy - log a line, prepend date
require 'date'
while( line = gets ) do
now=Time.new().utc().strftime("%y%m%d%H%M%S")
print "#{now}: #{line}";
end
蟒蛇呢?
#!/bin/env python
#loggy - log a line, prepend date
#see: http://docs.python.org/2/library/datetime.html
from datetime import datetime
#do you prefer fileinput or sys?
#import fileinput
#for line in fileinput.input():
# now=datetime.now()
# print now.strftime("%y%m%d%H%M%S"), ": ", line;
import sys
for line in sys.stdin:
now=datetime.now()
print now.strftime("%y%m%d%H%M%S"), ": ", line;
和 C,
#include <stdio.h>
#include <time.h> //strftime, time_t time() to second resolution
#include <sys/time.h> //gettimeofday, microsecond resolution
//size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
//int gettimeofday(struct timeval *tv, struct timezone *tz);
//use reentrant versions,
//struct tm *gmtime_r(const time_t *timep, struct tm *result);
//struct tm *localtime_r(const time_t *timep, struct tm *result);
int main()
{
char buffer[4096]; //largest log entry
char datestr[64]; //space for %t%m%d%H%M%S datetimestamp
time_t prev=0;
//struct timeval tv;
time_t tx;
struct tm nowtm;
while(fgets(buffer,sizeof(buffer),stdin))
{
tx = time(NULL);
//or, microsecond resolution
//gettimeofday(&tv,NULL);
if(tx != prev)
{
strftime(datestr,sizeof(datestr),"%y%m%d%H%M%S",gmtime_r(&tx, &nowtm));
//strftime(datestr,sizeof(datestr),"%y%m%d%H%M%S",localtime_r(&tx, &nowtm));
prev = tx;
}
printf("%s: %s",datestr,buffer);
}
}
有人想提供 awk 版本吗?