aboutsummaryrefslogtreecommitdiff
blob: cba98f395900e7a3002e9a4ef3f021682bec65c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

from __future__ import unicode_literals

import io
import sys
import time
import portage
from portage import os
from portage import _encodings
from portage import _unicode_decode
from portage import _unicode_encode
from portage.data import secpass
from portage.output import xtermTitle

# We disable emergelog by default, since it's called from
# dblink.merge() and we don't want that to trigger log writes
# unless it's really called via emerge.
_disable = True
_emerge_log_dir = '/var/log'

def emergelog(xterm_titles, mystr, short_msg=None):

	if _disable:
		return

	mystr = _unicode_decode(mystr)

	if short_msg is not None:
		short_msg = _unicode_decode(short_msg)

	if xterm_titles and short_msg:
		if "HOSTNAME" in os.environ:
			short_msg = os.environ["HOSTNAME"]+": "+short_msg
		xtermTitle(short_msg)
	try:
		file_path = os.path.join(_emerge_log_dir, 'emerge.log')
		existing_log = os.path.exists(file_path)
		mylogfile = io.open(_unicode_encode(file_path,
			encoding=_encodings['fs'], errors='strict'),
			mode='a', encoding=_encodings['content'],
			errors='backslashreplace')
		if not existing_log:
			portage.util.apply_secpass_permissions(file_path,
				uid=portage.portage_uid, gid=portage.portage_gid,
				mode=0o660)
		mylock = portage.locks.lockfile(file_path)
		try:
			mylogfile.write("%.0f: %s\n" % (time.time(), mystr))
			mylogfile.close()
		finally:
			portage.locks.unlockfile(mylock)
	except (IOError,OSError,portage.exception.PortageException) as e:
		if secpass >= 1:
			portage.util.writemsg("emergelog(): %s\n" % (e,), noiselevel=-1)