博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

CalmArrow

【打好基础】全力以赴,顺其自然【每天进步一点点】
  piaoxiang.cublog.cn

关于作者
姓名:CalmArrow(lqm)
职业:硕士在读
位置:山东济南
研究:嵌入式系统设计
联系:calmarrow@gmail.com

信仰:
    1、永远保持积极向上(积极的心态,积极的思考,积极的行动),享受生活给予的一切!
    2、做正确的事,正确的做事;用心去做,做到最好!
    3、如果你觉得不幸福,那么请你把幸福的门槛降低一些,不要把幸福拒之门外。。。

目标:
    软硬结合,打好基础,提高学习能力,完善知识体系,建立核心优势。

方法:
    理论与实践相结合
    深度与广度相结合
    理解与记忆相结合

说明:
    本Blog仅供学习之用,转载文章如涉及版权,请通知。原创作品如转载,请注明出处。
|| << >> ||
我的分类


处理日志文件的演示程序
    在嵌入式开发过程中,我想在调试模式下把输出内容重定向到一个文件中。输出量不大时没有问题,但是时间长了,这个文件会浪费SDRAM空间。想到的方法是,定期处理该日志文件,只保留指定最新内容。下面写了一个演示bash shell程序。主要是把指定文件复制为源文件的两倍,以此为测试文件,然后处理缩减为源文件的大小。实际应用中,可以设置一个日志文件容量的上限,然后递交一个crontab。就可以完成实际工作了。
 
   bash shell演示程序(2007-02-09)如下:
 

#!/bin/sh
# Copyright (c) 2007, Shandong University
# All rights reserved.
#
# filename: handle_logfile
# description: to give an example that demonstrates how to
#              handle the logfile.
# Author: lqm


# One and only one parameter
if [ $# != 1 ]; then
        echo "Usage: `basename $0` [filename]"
        exit 1
fi

# Source file must exist
if [ -f $1 ]; then
        echo "$1 exists"
else
        echo "$1 does not exist"
        exit 1
fi

# Get the number of lines
LINE=`wc $1 | awk '{ print $1 }'`
SIZE=`wc $1 | awk '{ print $3 }'`
echo "line:$LINE, size:$SIZE"

# Create the test file
cp $1 test
cat $1 >> test

# Handle
find test -size +${SIZE}c -exec echo "Find the desired logfile" {} \; \
  && tail -n $LINE test >tmp \
  && rm -f test \
  && mv tmp test \
  && diff $1 test \
  && echo "Succeed!"

执行结果:

[armlinux@lqm practice]$ ./handle_log
Usage: handle_log [filename]
[armlinux@lqm practice]$ ./handle_log abc
abc does not exist
[armlinux@lqm practice]$ ./handle_log mylogin
mylogin exists
line:16, size:268
Find the desired logfile test

发表于: 2007-02-09,修改于: 2007-11-19 17:00,已浏览1220次,有评论0条 推荐 投诉


网友评论
 发表评论