Electron cyclotron emission can be a sensitive indicator of nonthermal electron distributions. A new, comprehensive ray-tracing and cyclotron emission code that is aimed at predicting and interpreting the cyclotron emission from tokamak plasmas is described. The radiation transfer equation is solved along Wentzel-Kramers-Brillouin (WKB) rays using a fully relativistic calculation of the emission and absorption from electron distributions that are gyrotropic and toroidally symmetric, but may be otherwise arbitrary functions of the constants of motion. Using a radial array of electron distributions obtained from a bounce-averaged Fokker-Planck code modeling dc electron field and electron cyclotron heating effects, the cyclotron emission spectra are obtained. A pronounced strong nonthermal cyclotron emission feature that occurs at frequencies relativistically downshifted to second harmonic cyclotron frequencies outside the tokamak is calculated, in agreement with experimental results from the DIII-D [J. L. Luxon and L. G. Davies, Fusion Technol. 8, 441 (1985)] and FT-1 [D. G. Bulyginsky et al., in Proceedings of the 15th European Conference on Controlled Fusion and Plasma Heating, Dubrovnik, 1988 (European Physical Society, Petit-Lancy, 1988), Vol. 12B, Part II, p. 823] tokamaks. The calculations indicate the presence of a strong loss mechanism that operates on electrons in the 100-150 keV energy range.